-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExchangeCmd.java
More file actions
44 lines (39 loc) · 1.37 KB
/
ExchangeCmd.java
File metadata and controls
44 lines (39 loc) · 1.37 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.awt.*;
/**
* ExchangeCmd.java
* Command class to perform an exchange command.
*
* Written by THC for CS 5 Lab Assignment 3.
* @author Thomas H. Cormen
*/
public class ExchangeCmd extends Command {
private Shape firstShape; // the first Shape clicked
/**
* When the mouse is clicked, find the frontmost Shape in the drawing
* that contains the mouse position. If there is such a Shape, then
* if this is the first click in the pair of clicks (indicated by
* firstShape being null), save it in firstShape. Otherwise, exchange
* the centers of this newly clicked Shape and firstShape.
*
* @param p the coordinates of the click
* @param dwg the drawing being clicked
*/
public void executeClick(Point p, Drawing dwg) {
// Find the frontmost shape containing p.
Shape s = dwg.getFrontmostContainer(p);
if (s != null) { // was there a Shape containing p?
if (firstShape == null)
firstShape = s; // save this Shape for when there's another click
else {
// We have two Shapes to exchange. Get their centers.
Point firstCenter = firstShape.getCenter();
Point secondCenter = s.getCenter();
// Exchange their centers.
firstShape.setCenter(secondCenter);
s.setCenter(firstCenter);
// Now we get to start all over.
firstShape = null;
}
}
}
}