Java Examples - Create different shapes in an Applet


Advertisements


Problem Description

How to create different shapes using Applet?

Solution

Following example demonstrates how to create an applet which will have a line, an Oval & a Rectangle using drawLine(), drawOval(, drawRect() methods of Graphics clas.

import java.applet.*;
import java.awt.*;

public class  Shapes extends Applet {
   int x = 300, y = 100, r = 50;
   public void paint(Graphics g) {
      g.drawLine(30,300,200,10);
      g.drawOval(x-r,y-r,100,100);
      g.drawRect(400,50,200,100);
   }
}

Result

The above code sample will produce the following result in a java enabled web browser.

A line , Oval & a Rectangle will be drawn in the browser.

java_applets.htm

Advertisements