Academic Java    Java Tutorial  >  Applets  >  drawRect() of squares

drawRect() of squares Example

Java drawRect() of squares EXAMPLE output Note that using drawRect(), drawing a rectangle with Graphics draws it with a width of one pixel.

As shown in the Graphics section, drawRect() using Graphics2D can provide lines with thickness.
// drawRect() Example 2
import java.awt.*;
import javax.swing.*;

class DrawRectExample2 extends JApplet {

   @Override public void paint(Graphics g) {
      super.paint(g);

      int size = 200;

      do {

         g.drawRect(150 - size/2, 150 - size/2, size, size);
         size = size - 20;

      } while (size > 0);

   }
}


* drawRect() fillRect() Examples