// MoveCmd.java // Command class to perform a move command. // Written by THC for CS 5 HW 3. // Modified by Scot Drysdale for VoronoiTool on 5/17/06 import java.awt.*; public class MoveCmd extends Command { private Point lastPoint; // the mouse position last time private Shape s; // which Shape is being moved // When the mouse is pressed, find the closest close Shape in the drawing // to the mouse position. If there is such a Shape, then // remember the Shape and the mouse position. public void executePress(Point p, Drawing dwg) { s = dwg.getCloseShape(p); // find close Shape to p if (s != null) // was there a Shape containing p? lastPoint = p; // yes, remember where p is } // When the mouse is dragged, if we had found a Shape when the mouse was // pressed, move the Shape by the difference between the current position of // the mouse and where it was last time. Then update so that the current mouse // position will be remembered for next time. public void executeDrag(Point p, Drawing dwg) { if (s != null) // did we find a Shape when the mouse was pressed? { s.move(p.x - lastPoint.x, p.y - lastPoint.y, lastPoint); // yes, so move it... lastPoint = p; // ...and remember current mouse position } } }