package interactors; import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * The MoveInteractor computes the new top/left of a selected object and * passes that information to the start, running, and stop actions */ public class MoveInteractor extends Interactor { // leftOffset and topOffset represent the offset of the mouse cursor // from the upper left corner of the selected object when the mouse // is first pressed over the object int leftOffset; int topOffset; public MoveInteractor(int start, int stop) { super(start, stop); } // compute the initial offset of the mouse from the upper left corner // of the selected object so that the position of the cursor with respect // to the upper left corner is maintained throughout the move interaction public void mousePressed(MouseEvent e) { int x = e.getX(); int y = e.getY(); leftOffset = x - (int)selectedObj.getX(); topOffset = y - (int)selectedObj.getY(); startAction((int)selectedObj.getX(), (int)selectedObj.getY(), e); } public void mouseReleased(MouseEvent e) { stopAction(e.getX() - leftOffset, e.getY() - topOffset, e); } public void mouseDragged(MouseEvent e) { runningAction(e.getX() - leftOffset, e.getY() - topOffset, e); } public void startAction(int left, int top, MouseEvent e) { // nothing to be done } public void runningAction(int left, int top, MouseEvent e) { // mark both the old and new locations of the selected object as // being damaged JPanel parentPanel = (JPanel)e.getSource(); parentPanel.repaint(selectedObj); selectedObj.setLocation(left, top); parentPanel.repaint(selectedObj); } public void stopAction(int left, int top, MouseEvent e) { // mark both the old and new locations of the selected object as // being damaged JPanel parentPanel = (JPanel)e.getSource(); parentPanel.repaint(selectedObj); selectedObj.setLocation(left, top); parentPanel.repaint(selectedObj); } }