Java Draw Rectangles


Java Draw Rectangles

Summary: In this tutorial "Java Draw Rectangles", you learn how much it is easy to draw rectangles in Java.

Drawing Rectangles

3 types of rectangles Java supports
  1. Right-angled rectangles
  2. Round-cornered rectangles
  3. 3-D Rectangles

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, outline) 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.

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.

import java.awt.*;
public class RectanglesDrawing extends Frame
{
  public RectanglesDrawing()
  {
    setTitle("Right-angled Drawing");
    setSize(475, 300);
    setVisible(true);
  }
  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);
  }
  public static void main(String args[])
  {
    new RectanglesDrawing();
  }
}


Java Draw Rectangles
Output screen of RectanglesDrawing.java of Java Draw Rectangles

Java Draw Rectangles

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

The frame you get do not close when clicked over the close icon on the title bar of the frame. It requires extra code.

Drawing Square

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

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

Leave a Comment

Your email address will not be published.