Java Draw 3 D Rectangles


Drawing Rectangles

Java graphics supports 3 types drawing rectangles.

  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 is 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 third type, drawing three-dimensional rectangles.

Drawing 3-D Rectangles

Java supports 3D rectangles also but the affect of third dimension is not much visible. As the elevation is very less, the affect is negligible. Architects say, green color gives good affect in elevations. For this reason, the graphics is set to green color in this program.

Supporting methods

  1. 1. void draw3DRect(int x, int y, int width, int height, boolean raised): This method draws an outline 3D rectangle with the above specified parameters. The last boolean parameter true indicates elevation above the drawing surface and false indicates etching (lowering) into the surface.
  2. 2. void fill3DRect(int x, int y, int width, int height, boolean raised): This method draws a solid 3D rectangle with the above specified parameters. The last boolean parameter true indicates elevation above the drawing surface and false indicates etching into the surface.

Java designers give the affect of 3D by drawing lighter and darker lines along the rectangle border.

import java.awt.*;
public class ThreeDRectangles extends Frame
{
  public ThreeDRectangles ()
  {
    setTitle("Drawing 3 D Rectangles");
    setSize(500, 500);
    setVisible(true);
  }
  public void paint(Graphics g)
  {
    g.setColor(Color.green);
    g.draw3DRect(60, 70, 150, 100, true);
    g.draw3DRect(240, 70, 150, 100, false);

    g.fill3DRect(60, 250, 150, 100, true);
    g.fill3DRect(240, 250, 150, 100, false);
  }
  public static void main(String args[])
  {
    new ThreeDRectangles();
  }
}

Java Draw 3 D Rectangles

g.draw3DRect(60, 70, 150, 100, true);

The draw3DReect() method draws a rectangle using the first four parameters and as the fourth parameter is true, draws the elevation affect by darkening some sides. The frame you get do not close when clicked over the close icon on the title bar of the frame. It requires extra code.

5 thoughts on “Java Draw 3 D Rectangles”

  1. Much appreciated! I was using the default color (black) and couldn’t see any 3D effect in my 3D rectangles. Started wondering what is broken on my system. I see yours are green, so I tried that. Yes! It turns out any color EXCEPT black lets you see the 3D effect. Even white.

      1. Yellow is fine. The only color that doesn’t show me a 3D effect (for draw3DRect) is black and I expect it isn’t supposed to work. Or perhaps the color difference is too subtle to notice on my laptop screen.

Leave a Comment

Your email address will not be published.