-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveCmd.java
More file actions
29 lines (29 loc) · 986 Bytes
/
MoveCmd.java
File metadata and controls
29 lines (29 loc) · 986 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.awt.*;
/**
* Subclass of Command that moves a shape when pressed and dragged.
* @author Jasper Bingham
*/
public class MoveCmd extends Command
{
private Shape selected = null;
private Point clickPoint;
private Point center;
private boolean move;
public void executePress(Point p, Drawing dwg) {
if(dwg.getFrontmostContainer(p) != null) //check if there is a shape under click point
{
selected = dwg.getFrontmostContainer(p);
center = selected.getCenter(); //center of clicked shape
clickPoint = p; //where the user presses
}
}
public void executeDrag(Point p, Drawing dwg) {
if(dwg.getFrontmostContainer(p) != null)
{
int deltaX = p.x - clickPoint.x; //change in X from pressed point to current point
int deltaY = p.y - clickPoint.y; //change in Y from pressed point to current point
Point newCenter = new Point(center.x + deltaX, center.y + deltaY); //shift center by change in X/Y
selected.setCenter(newCenter);
}
}
}