In this shapeDrawingApplet program we see how to draw the different shapes like line, circle and rectangle.
GraphicsObject.drawLine() :
The drawLine() method is used in this program to draw the line. Here is the syntax:
GraphicsObject.drawLine(int x_coordinate, int y_coordinate, int x1_coordinate, int y1_coordinate);
GraphicsObject.drawString() :
The drawSring() method is used in this program to draw string . Here is the syntax :
GraphicsObject.drawString(String str, int x_coordinate, int y_coordinate);
GraphicsObject.drawOval() :
The drawOval() method is used to draws the circle. Here is the syntax :
GraphicsObject.drawOval(int x_coordinate, int y_coordinate, int width, int height);
GraphicsObject.drawRect() :
The drawRect() method is used to draws the rectangle. Here is the syntax :
GraphicsObject.drawRect(int x_coordinate, int y_coordinate, int Wdth, int height);
Here is the shapeDrawingApplet program java code :.
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class shapeDrawingApplet extends Applet
{
public static void main(String[] args)
{
Frame DrawingApplet = new Frame("DrawingApplet");
DrawingApplet.setSize(350, 250);
Applet shapeDrawingApplet = new shapeDrawingApplet();
DrawingApplet.add(shapeDrawingApplet);
DrawingApplet.setVisible(true);
DrawingApplet.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0); }
});
}
public void paint(Graphics g)
{
int x1[] = { 120, 125, 150, 150, 200, 200 };
int y1[] = { 175, 100, 100, 175, 175, 200 };
setBackground(Color.yellow);
// draws String
g.setColor(Color.blue); // Now we tell g to change the color
g.setFont(new Font("Arial",Font.BOLD,14)); // Now we tell g to change the font
g.drawString("Shape Drawing Applet", 100, 100);
// draws a Line
g.drawLine(90, 135, 90, 180);
// draws a Oval
g.setColor(Color.black);
g.drawOval(0, 10, 100, 100);
// draws a Rectangle
g.setColor(Color.black);
g.drawRect(190, 50, 100, 100);
// fill an Arc
g.setColor(Color.black);
g.fillArc(120,120,60,60,0,360);
// draws a Polygon
g.drawPolygon(x1, y1, 6);
}
}