Graphics Programming Using Applet In Java

Introduction

In this article we discuss graphics programming using Applets in Java. Also what Applets are and their advantages and disadvantages.

Applet

First we have a look what Applets are.

A Java Applet is a small application written in Java, and delivered to users in the form of bytecode. Java Applets can be written in any programming language that compiles to Java byte-code. An Applet is a special type of program that is embedded in the webpage to generate the dynamic content and that runs inside the browser and works at the client side. The user launches the Java Applet from a web page. Java Applets were introduced in the first version of the Java language in 1995.

Advantages

Java Applets have the following advantages:

  1. They are simple to make and they are supported by most web browsers.
  2. They are faster in nature.
  3. At the same time the same Applet can work on "all" installed versions of Java.
  4. An Applet can be debug and developed directly, by simply creating a main routine.
  5. Applets that are not trusted have no connection to the local machine, they can only access the server from which it comes
  6. Makes a connection between server and client.

Disadvantages

Java Applets have following disadvantages:

  1. for properly executing Applets we requires the Java plug-in.
  2. mobile browsers running Android or Apple iOS don't run Java Applets.
  3. In client-side scripting, achieving the desired goal is impossible for security reasons.
  4. Some Applets require a specific JRE. This is discouraged.
  5. If an Applet requires a newer JRE than available on the system, or a specific JRE then the user running it for the first time will need to wait for the large JRE download to complete.

Graphics in Applets

The java.awt.Graphics class provides many methods for graphics programming.

Some commonly used public methods in the Graphics class:

  1. abstract void setClor(Color clr) sets the Graphics current color to the specified color.
  2. abstract void drawString(String strng, int a, int b) draws the specified string.
  3. void drawRect(int a, int b, int wdth, int hgt) draws a rectangle with the specified width and height.
  4. abstract void fillRect(int a, int b, int wdth, int hgt) fills a rectangle with the default color and specified width and height.
  5. abstract void drawOval(int a, int b, int wdth, int hgt) draws an oval.
  6. abstract void fillOval(int a, int b, int wdth, int hgt) fills an oval with the default color.
  7. abstract boolean drawImage(Image img, int a, int b, int hgt, ImageObserver obsrvr) draws the specified image.
  8. abstract void drawArc(int a, int b, int wdth, int hgt, int startAngle, intarcAngle) draws a circular or elliptical arc.
  9. abstract void fillarc(int a, int b, int wdth, int hgt, int startAngle, int arcAngle) fills a circular or elliptical arc.
  10. abstract void  drawLine(int a1, int b1, int a2, int b2) draws a line.
  11. abstract void setFont(Font fnt) sets the current graphics font to the specified font.

1. Draw a simple string

In this program, we show how to design a string in graphics programming.

import java.awt.*;

import java.applet.Applet;

public class GrpStringEx extends Applet

  {

    public void paint(Graphics grp)

      {

        grp.setColor(Color.blue);

        grp.drawString("welcome-to-appletworld-in-java", 150, 150);

      }

  }

/*

<applet code="GrpStringEx.class" width="400" height="400">

</applet

*/

Output

To execute our code by the appletviewer tool, write in the command prompt:

javac GrpStringEx.java
appletviewer GrpStringEx.java

fig-1.jpg

2. Draw a rectangle

In this example, we draw a simple rectangle.

import java.awt.*;

import java.applet.Applet;

public class GrpRectEx extends Applet

  {

    public void paint(Graphics grp)

      {

        grp.setColor(Color.blue);

        grp.drawRect(100,50,150,150);

      }

  }

/*

<applet code="GrpRectEx.class" width="400" height="400">

</applet

*/

Output

fig-2.jpg

3. Fill color in rectangle

In this example, we fill in the color of our rectangle usnig the grp.fillRect method.

import java.awt.*;

import java.applet.Applet;

public class GrpRectEx extends Applet

  {

    public void paint(Graphics grp)

      {

        grp.setColor(Color.blue);

        grp.drawRect(100,50,150,150);

        grp.fillRect(100,50,150,150);

      }

  }

/*

<applet code="GrpRectEx.class" width="400" height="400">

</applet

*/

Output

FIG-3.jpg

4. Draw an oval and an arc

In this example, we create an oval and an arc with specified co-ordinates.

import java.awt.*;

import java.applet.Applet;

public class GrpOvalEx extends Applet

{

public void paint(Graphics grp)

{

grp.setColor(Color.blue);

grp.drawOval(100,50,150,150);

grp.drawArc(100,150,150,150,0,180);

}

}

/*

<applet code="GrpOvalEx.class" width="400" height="400">

</applet

*/

Output

fig-4.jpg

5. fill color in oval and arc

In this example, we fill an oval and an arc with a color using the fillOval and fillArc methods.

import java.awt.*;

import java.applet.Applet;

public class GrpOvalEx extends Applet

  {

    public void paint(Graphics grp)

      {

        grp.setColor(Color.blue);

        grp.fillOval(100,50,150,150);

        grp.setColor(Color.red);

        grp.fillArc(100,150,150,150,0,180);

      }

  }

/*

<applet code="GrpOvalEx.class" width="400" height="400">

</applet

*/

Output

fig-5.jpg

 6. Draw a line

In this example, we draw a simple line with specified co-ordinates.

import java.awt.*;

import java.applet.Applet;

public class GrpLineEx extends Applet

  {

    public void paint(Graphics grp)

      {

        grp.setColor(Color.blue);

        grp.drawLine(50,30,400,30);

      }

  }

/*

<applet code="GrpLineEx.class" width="400" height="400">

</applet

*/ 

Output

fig-6.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all