Source Code:
import objectdraw.*;
import java.awt.*;
/**
* Lisa Ballesteros adapted from JAEA
* A program that lets the mouse drag a box around the screen.
*
*/
public class WhatADrag extends WindowController {
// Coordinates of original box location
private static final double START_LEFT = 100;
private static final double START_TOP = 100;
// Dimensions of box
private static final double BOX_WIDTH = 150;
private static final double BOX_HEIGHT = 150;
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;
// 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;
}
}
}