// CS 134 demo. Second stage in constructing a program // to drag a box around on the screen. import objectdraw.*; import java.awt.*; public class MouseDrag extends WindowController { // Starting position for box private static final int START_LEFT = 200, START_TOP = 100, // dimensions of the box BOX_HEIGHT = 30, BOX_WIDTH = 30; private FilledRect box; // box to be dragged // point where mouse was last seen private Location lastPoint; // make the box public void begin() { box = new FilledRect(START_LEFT, START_TOP, BOX_WIDTH, BOX_HEIGHT, canvas); } // Save starting point public void onMousePress(Location point) { lastPoint = point; } // if mouse is in box, then drag the box public void onMouseDrag(Location point) { if ( box.contains( lastPoint) ) { box.move( point.getX() - lastPoint.getX(), point.getY() - lastPoint.getY() ); lastPoint = point; } } }