public class RectangleComponentFrame extends JFrame { private RectangleComponent component; public RectangleComponentFrame(int w, int h) { // Initialize JFrame parent and set the frame title super("Rectangle Mover"); // Create a rectangle component component = new RectangleComponent(); // Create and add a mouse listener for the component component.addMouseListener( new MouseAdapter() { public void mousePressed(MouseEvent event) { int x = event.getX(); int y = event.getY(); component.moveTo(x, y); } }); // Add the component to this frame add(component); // Set the size of this frame setSize(w,h); } }