      1  import java.awt.event.MouseListener;

      2  import java.awt.event.MouseEvent;

      3  import javax.swing.JFrame;

      4

      5  /**

      6     This program displays a RectangleComponent.

      7  */

      8  public class RectangleComponentViewer

      9  {

     10     private static final int FRAME_WIDTH = 300;

     11     private static final int FRAME_HEIGHT = 400;

     12

     13     public static void main(String[] args)

     14     {

     15        final RectangleComponent component = new RectangleComponent();

     16

     17        // Add mouse press listener

     18

     19        class MousePressListener implements MouseListener

     20        {

     21           public void mousePressed(MouseEvent event)

     22           {

     23              int x = event.getX();

     24              int y = event.getY();

     25              component.moveTo(x, y);

     26           }

     27

     28           // Do-nothing methods

     29           public void mouseReleased(MouseEvent event) {}

     30           public void mouseClicked(MouseEvent event) {}

     31           public void mouseEntered(MouseEvent event) {}

     32           public void mouseExited(MouseEvent event) {}

     33        }

     34

     35        MouseListener listener = new MousePressListener();

     36        component.addMouseListener(listener);

     37

     38        JFrame frame = new JFrame();

     39        frame.add(component);

     40

     41        frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);

     42        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

     43        frame.setVisible(true);

     44     }

     45  }