// MoveCmd.java // Command class to perform a move command. // Written by THC for CS 5 Lab Assignment 3. 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 frontmost Shape in the drawing // that contains the mouse position. If there is such a Shape, then // remember the Shape and the mouse position. public void executePress(Point p, Drawing dwg) { // Find the frontmost Shape containing p. s = dwg.getFrontmostContainer(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); // yes, so move it... lastPoint = p; // ...and remember current mouse position } } }