Revision 776
Code cleanup and documentation
RobotIcon.java | ||
---|---|---|
1 | 1 |
|
2 |
import javax.swing.*; |
|
3 |
import javax.swing.event.*; |
|
4 |
import javax.imageio.*; |
|
5 | 2 |
import java.awt.*; |
6 |
import java.awt.image.*; |
|
7 |
import java.awt.event.*; |
|
8 |
import java.net.*; |
|
9 |
import java.io.*; |
|
10 |
import java.util.*; |
|
11 | 3 |
|
12 | 4 |
/** |
13 | 5 |
* RobotIcon class |
14 | 6 |
* Provides a means for graphically representing and keeping track of webcam bots. |
15 | 7 |
*/ |
16 | 8 |
public class RobotIcon { |
9 |
private int id; // the XBee ID of the robot |
|
10 |
private Color color; // the color used for painting the robot on screen. |
|
11 |
private Point location; // the position of the robot in the coordinate system of the webcam image |
|
12 |
private Point destination; // the destination of the robot, if homing or charging |
|
13 |
private int battery; // the battery level, if available |
|
17 | 14 |
|
18 |
public int x, y; |
|
19 |
public int destx, desty; |
|
20 |
public int id; |
|
21 |
public Color color; |
|
22 |
public int battery; |
|
23 |
|
|
24 | 15 |
public RobotIcon (int id, int x, int y) { |
25 |
this.color = Color.RED; |
|
26 |
this.x = x; |
|
27 |
this.y = y; |
|
28 |
this.id = id; |
|
29 |
this.destx = -1; |
|
30 |
this.desty = -1; |
|
31 |
this.battery = -1; |
|
16 |
setID(id); |
|
17 |
setColor(Color.RED); |
|
18 |
setLocation(x, y); |
|
19 |
clearDestination(); |
|
20 |
setBattery(-1); |
|
32 | 21 |
} |
22 |
|
|
23 |
/** |
|
24 |
* Returns the ID of this RobotIcon. |
|
25 |
*/ |
|
26 |
public int getID () { |
|
27 |
return id; |
|
28 |
} |
|
29 |
|
|
30 |
/** |
|
31 |
* Sets the ID of this RobotIcon. |
|
32 |
*/ |
|
33 |
public void setID (int newid) { |
|
34 |
this.id = newid; |
|
35 |
} |
|
36 |
|
|
37 |
/** |
|
38 |
* Returns the Color used for representing this RobotIcon on screen. |
|
39 |
*/ |
|
40 |
public Color getColor () { |
|
41 |
return color; |
|
42 |
} |
|
43 |
|
|
44 |
/** |
|
45 |
* Sets the Color used for representing this RobotIcon on screen. |
|
46 |
*/ |
|
47 |
public void setColor (Color newcolor) { |
|
48 |
this.color = newcolor; |
|
49 |
} |
|
50 |
|
|
51 |
/** |
|
52 |
* Returns the battery level of this RobotIcon. |
|
53 |
* TODO: what numbering system is this? |
|
54 |
*/ |
|
55 |
public int getBattery () { |
|
56 |
return battery; |
|
57 |
} |
|
58 |
|
|
59 |
/** |
|
60 |
* Sets the battery level of this RobotIcon. |
|
61 |
* TODO: what numbering system is this? |
|
62 |
*/ |
|
63 |
public void setBattery (int newbattery) { |
|
64 |
this.battery = newbattery; |
|
65 |
} |
|
66 |
|
|
67 |
/** |
|
68 |
* Returns the location of this RobotIcon as a Point. The location is specified |
|
69 |
* in the coordinate system of the original webcam image, NOT the coordinate |
|
70 |
* system of the scaled image as seen on screen. |
|
71 |
*/ |
|
72 |
public Point getLocation () { |
|
73 |
return location; |
|
74 |
} |
|
33 | 75 |
|
34 | 76 |
/** |
35 |
* Relocates this RobotIcon to a new coordinate point. |
|
36 |
* |
|
77 |
* Sets the location of this RobotIcon. The location should be specified in the |
|
78 |
* coordinate system of the original webcam image, NOT the coordinate system of |
|
79 |
* the scaled image as seen on screen. |
|
37 | 80 |
*/ |
38 |
public void move (int newX, int newY) { |
|
39 |
this.x = newX; |
|
40 |
this.y = newY; |
|
81 |
public void setLocation (int x, int y) { |
|
82 |
location.setLocation(x, y); |
|
41 | 83 |
} |
84 |
|
|
85 |
/** |
|
86 |
* Sets the location of this RobotIcon. The location should be specified in the |
|
87 |
* coordinate system of the original webcam image, NOT the coordinate system of |
|
88 |
* the scaled image as seen on screen. |
|
89 |
*/ |
|
90 |
public void setLocation (Point p) { |
|
91 |
location.setLocation(p); |
|
92 |
} |
|
93 |
|
|
94 |
/** |
|
95 |
* Returns the destination of this RobotIcon as a Point. The location is specified |
|
96 |
* in the coordinate system of the original webcam image, NOT the coordinate |
|
97 |
* system of the scaled image as seen on screen. If the RobotIcon has no current |
|
98 |
* destination, then this method returns null. |
|
99 |
*/ |
|
100 |
public Point getDestination () { |
|
101 |
if (!hasDestination()) |
|
102 |
return null; |
|
103 |
return destination; |
|
104 |
} |
|
105 |
|
|
106 |
/** |
|
107 |
* Determines whether this RobotIcon currently has a destination, e.g., for homing |
|
108 |
* to a specific point in the environment or finding a charging bay. |
|
109 |
*/ |
|
110 |
public boolean hasDestination () { |
|
111 |
return (destination.x >= 0 && destination.y >= 0); |
|
112 |
} |
|
113 |
|
|
114 |
/** |
|
115 |
* Sets the destination of this RobotIcon for the purposes of homing to a specific |
|
116 |
* point in the environment of finding a charging bay. The destination point should |
|
117 |
* be specified in the coordinate system of the original webcam image, NOT the |
|
118 |
* coordinate system of the scaled image as seen on screen. |
|
119 |
*/ |
|
120 |
public void setDestination (int x, int y) { |
|
121 |
destination.setLocation(x, y); |
|
122 |
} |
|
123 |
|
|
124 |
/** |
|
125 |
* Sets the destination of this RobotIcon for the purposes of homing to a specific |
|
126 |
* point in the environment of finding a charging bay. The destination point should |
|
127 |
* be specified in the coordinate system of the original webcam image, NOT the |
|
128 |
* coordinate system of the scaled image as seen on screen. |
|
129 |
*/ |
|
130 |
public void setDestination (Point p) { |
|
131 |
destination.setLocation(p); |
|
132 |
} |
|
133 |
|
|
134 |
/** |
|
135 |
* Clears the destination Point for this RobotIcon, indicating that the robot is |
|
136 |
* no longer homing. |
|
137 |
*/ |
|
138 |
public void clearDestination () { |
|
139 |
setDestination(-1, -1); |
|
140 |
} |
|
42 | 141 |
|
43 | 142 |
/** |
44 | 143 |
* Determines whether a given point is within the rectangle that circumscribes the |
45 |
* robot's circlular icon. Used for clicking on robots in webcam view. |
|
46 |
* |
|
144 |
* robot's circlular icon. Used for click detection on robots in webcam view. The |
|
145 |
* parameter point should be specified in the coordinate system of the original |
|
146 |
* webcam image, NOT the coordinate system of the scaled image as seen on screen. |
|
47 | 147 |
*/ |
48 | 148 |
public boolean contains (int px, int py) { |
49 |
int radius = ColonetConstants.ROBOT_RADIUS; |
|
50 |
Rectangle rect = new Rectangle(x-radius, y-radius, 2*radius, 2*radius); |
|
51 |
return rect.contains(px, py); |
|
149 |
return this.getBoundingRectangle().contains(px, py); |
|
52 | 150 |
} |
151 |
|
|
152 |
/** |
|
153 |
* Returns the Rectangle object that circumscribes the RobotIcon identifier circle |
|
154 |
* that is painted on screen. The Rectangle is in the coordinate system of the |
|
155 |
* original webcam image NOT the coordinate system of the scaled image as seen |
|
156 |
* on screen. |
|
157 |
*/ |
|
158 |
public Rectangle getBoundingRectangle () { |
|
159 |
int radius = ColonetConstants.ROBOT_RADIUS; |
|
160 |
return new Rectangle(location.x-radius, location.y-radius, 2*radius, 2*radius); |
|
161 |
} |
|
53 | 162 |
|
163 |
/** |
|
164 |
* Provides a text representation of this RobotIcon object |
|
165 |
*/ |
|
54 | 166 |
public String toString () { |
55 |
String s = "RobotIcon at (" + x + "," + y + "), id " + id;
|
|
167 |
String s = "RobotIcon at (" + location.x + "," + location.y + "), id " + id;
|
|
56 | 168 |
return s; |
57 | 169 |
} |
58 | 170 |
} |
Also available in: Unified diff