import objectdraw.*; import java.awt.*; public class MouseDrag2 extends WindowController{ // Starting position for box private static final int START_LEFT = 200, START_TOP = 100, BOX_HEIGHT = 30, // dimensions of the box BOX_WIDTH = 30; private FilledRect box; // box to be dragged private Location lastPoint; // point where mouse was last seen // whether the box has been grabbed by the mouse private boolean boxGrabbed = false; // make the box public void begin() { box = new FilledRect(START_LEFT, START_TOP, BOX_WIDTH, BOX_HEIGHT, canvas); } // Save starting point and whether point was in box public void onMousePress(Location point) { lastPoint = point; boxGrabbed = box.contains(point); } // if mouse is in box, then drag the box public void onMouseDrag(Location point) { if ( boxGrabbed ) { box.move( point.getX() - lastPoint.getX(), point.getY() - lastPoint.getY() ); lastPoint = point; } } }