Java Draw Rectangle Applet graphics


Java supports graphics through applications (extends Frame) and applets (extending Applet). We have seen drawing rectangles through applications and now let us convert the same rectangle application into an applet program. To go throug this code, it is required to have the knowledge of applets, applet life cycle methods and drawing strings on applet window.

Drawing Rectangles (Applet graphics)

Java graphics supports 3 types of drawing rectangles.

  1. Right-angled rectangles (as application)
  2. Round-cornered rectangles (as application)
  3. 3-D Rectangles (as application)

Java requires width and height of the rectangle and x and y coordinates where rectangle (top left corner position) is to be drawn. Java does not include a special method, something like drawSquare(), to draw a square. Square can be drawn with rectangle method only by keeping width and height same.

Every closed figure in Java graphics comes with two methods – drawXXX() that draws outline (not filled with some color) figure and fillXXX() that draws filled (solid) figure. The default drawing color is black with white background which can be changed.

Now let us explore the first type, drawing right-angled rectangles in applets.

Drawing Right-angled Rectangles

Java comes with two methods to draw right-angled rectangles.

Supporting methods in Graphics class

  • void drawRect(int x, int y, int width, int height): Draws an outline rectangle with the left-top coordinates of x and y and with the width and height specified.
  • void fillRect(int x, int y, int width, int height): Draws a solid rectangle with the left-top coordinates of x and y and with the width and height specified.

Following program draws two right-angled rectangles – outline and solid in applet window.

Applet file name: RectanglesDrawing.java

import java.awt.*;
import java.applet.*;
public class RectanglesDrawing extends Applet
{
  public void paint(Graphics g)
  {
    g.setColor(Color.blue);
    g.drawRect(50, 80, 150, 100);
    g.setColor(Color.magenta);
    g.fillRect(230, 80, 150, 100);
  }
}

HTML file to runt the applet. File Name: DR.html


Java Draw Rectangle Applet graphics

g.setColor(Color.blue);
g.drawRect(50, 80, 150, 100);

We know, the setColor() method of Graphics class sets the drawing color. In the above statement, the color is set to blue (one of the 13 predefined colors) and this color will be effective until changed again.

drawRect() method takes 4 parameters. 50 and 80 are the x and y positions that represents the top-left coordinates of the rectangle. 150 and 100 are the width and height of the rectangle. The method draws an outline rectangle in blue color.

g.setColor(Color.magenta);
g.fillRect(230, 80, 150, 100);

fillRect() method draws a solid rectangle filled with magenta (one of the 13 predefined colors) color. 230, 80 are x, y coordinates and 150, 100 are width and height of the rectangle

Drawing Square

g.drawRect(50, 80, 150, 150);

The above statement draws a square. Observe, the width and height are same of 150 pixels.

Other applet graphics examples are Drawing Polygons, Drawing Circles etc.

Leave a Comment

Your email address will not be published.