01: import java.awt.Graphics;
02: import java.awt.Graphics2D;
03: import java.awt.Rectangle;
04: import javax.swing.JComponent;
05: 
06: /**
07:    This component lets the user move a rectangle by clicking
08:    the mouse.
09: */
10: public class RectangleComponent extends JComponent
11: {  
12:    public RectangleComponent()
13:    {  
14:       // The rectangle that the paint method draws
15:       box = new Rectangle(BOX_X, BOX_Y, 
16:             BOX_WIDTH, BOX_HEIGHT);         
17:    }
18: 
19:    public void paintComponent(Graphics g)
20:    {  
21:       super.paintComponent(g);
22:       Graphics2D g2 = (Graphics2D) g;
23: 
24:       g2.draw(box);
25:    }
26: 
27:    /**
28:       Moves the rectangle to the given location.
29:       @param x the x-position of the new location
30:       @param y the y-position of the new location
31:    */
32:    public void moveTo(int x, int y)
33:    {
34:       box.setLocation(x, y);
35:       repaint();      
36:    }
37: 
38:    private Rectangle box;
39: 
40:    private static final int BOX_X = 100;
41:    private static final int BOX_Y = 100;
42:    private static final int BOX_WIDTH = 20;
43:    private static final int BOX_HEIGHT = 30;
44: }