RectangleComponent.java.html

  1  import java.awt.Graphics;
  2  import java.awt.Graphics2D;
  3  import java.awt.Rectangle;
  4  import javax.swing.JComponent;
  5  
  6  /*
  7     A component that draws two rectangles.
  8  */
  9  public class RectangleComponent extends JComponent
 10  {  
 11     public void paintComponent(Graphics g)
 12     {  
 13        // Recover Graphics2D
 14        Graphics2D g2 = (Graphics2D) g;
 15  
 16        // Construct a rectangle and draw it
 17        Rectangle box = new Rectangle(5, 10, 20, 30);
 18        g2.draw(box);
 19  
 20        // Move rectangle 15 units to the right and 25 units down
 21        box.translate(15, 25);
 22  
 23        // Draw moved rectangle
 24        g2.draw(box);
 25     }
 26  }