root / trunk / code / projects / colonet / client / Colonet.java @ 702
History | View | Annotate | Download (37.4 KB)
1 |
import javax.swing.*; |
---|---|
2 |
import javax.swing.event.*; |
3 |
import javax.imageio.*; |
4 |
import java.awt.*; |
5 |
import java.awt.image.*; |
6 |
import java.awt.event.*; |
7 |
import java.net.*; |
8 |
import java.io.*; |
9 |
import java.util.*; |
10 |
|
11 |
/**
|
12 |
* The Colonet Graphical User Interface Applet for use locally and over an internet connection.
|
13 |
* @author Gregory Tress
|
14 |
*
|
15 |
* To generate javadoc on this file or other java files, use javadoc *.java -d doc, where doc
|
16 |
* is the name of the folder into which the files should be written.
|
17 |
*/
|
18 |
public class Colonet extends JApplet implements ActionListener, MouseInputListener, KeyListener { |
19 |
|
20 |
// Connection
|
21 |
JTextField txtHost;
|
22 |
JTextField txtPort;
|
23 |
JButton btnConnect;
|
24 |
JButton btnGetXBeeIDs;
|
25 |
JLabel lblConnectionStatus;
|
26 |
JTextArea txtInfo;
|
27 |
JPanel panelConnect;
|
28 |
JPanel panelServerInterface;
|
29 |
Socket socket;
|
30 |
DataUpdater dataUpdater; |
31 |
|
32 |
// Control
|
33 |
JTabbedPane tabPaneControl;
|
34 |
JPanel panelRobotControl;
|
35 |
JPanel panelRobotDirection;
|
36 |
JPanel panelRobotDirectionButtons;
|
37 |
JPanel panelRobotCommands;
|
38 |
JButton btnF, btnB, btnL, btnR, btnActivate;
|
39 |
JLabel lblBattery;
|
40 |
JLabel lblSelected;
|
41 |
BatteryIcon batteryIcon; |
42 |
JPanel panelBattery;
|
43 |
VectorController vectorController; |
44 |
BufferedImage imageVectorControl;
|
45 |
JButton btnAssignID;
|
46 |
JButton btnLocateStation;
|
47 |
boolean setStation;
|
48 |
ChargingStation station; |
49 |
boolean setWaypoint;
|
50 |
int setWaypointID;
|
51 |
JButton btnSetBounds;
|
52 |
JButton btnClearBounds;
|
53 |
RobotBoundary boundary; |
54 |
JButton btnCommand_MoveTo;
|
55 |
JButton btnCommand_MoveAll;
|
56 |
JButton btnCommand_StopTask;
|
57 |
JButton btnCommand_ResumeTask;
|
58 |
JButton btnCommand_ChargeNow;
|
59 |
JButton btnCommand_StopCharging;
|
60 |
|
61 |
// Task Manager
|
62 |
JPanel panelTaskManager;
|
63 |
JScrollPane spTaskManager;
|
64 |
JPanel panelTaskManagerControls;
|
65 |
JPanel panelTaskManagerControlsPriority;
|
66 |
DefaultListModel taskListModel;
|
67 |
JList taskList;
|
68 |
JButton btnAddTask;
|
69 |
JButton btnRemoveTask;
|
70 |
JButton btnMoveTaskUp;
|
71 |
JButton btnMoveTaskDown;
|
72 |
JButton btnUpdateTasks;
|
73 |
TaskAddWindow taskAddWindow; |
74 |
|
75 |
// Webcam
|
76 |
WebcamPanel panelWebcam; |
77 |
GraphicsConfiguration gc;
|
78 |
JTabbedPane tabPaneMain;
|
79 |
WebcamLoader webcamLoader; |
80 |
|
81 |
// Robots
|
82 |
volatile int selectedBot; //the user has selected this bot graphically |
83 |
volatile RobotList robotIcons; //contains boundary shapes around bots for click detection |
84 |
volatile int[] xbeeID; |
85 |
|
86 |
Colonet self = this;
|
87 |
volatile ColonetServerInterface csi;
|
88 |
|
89 |
public void init () { |
90 |
// Set the default look and feel
|
91 |
String laf = ColonetConstants.LOOK_AND_FEEL;
|
92 |
try {
|
93 |
UIManager.setLookAndFeel(laf);
|
94 |
} catch (UnsupportedLookAndFeelException exc) { |
95 |
System.err.println ("Warning: UnsupportedLookAndFeel: " + laf); |
96 |
} catch (Exception exc) { |
97 |
System.err.println ("Error loading " + laf + ": " + exc); |
98 |
} |
99 |
|
100 |
// We should invoke and wait to avoid browser display difficulties
|
101 |
Runnable r = new Runnable() { |
102 |
public void run() { |
103 |
createAndShowGUI(); |
104 |
} |
105 |
}; |
106 |
|
107 |
try {
|
108 |
SwingUtilities.invokeAndWait(r);
|
109 |
} catch (InterruptedException e) { |
110 |
//Not really sure why we would be in this situation
|
111 |
System.out.println("InterruptedException in init: " + e); |
112 |
} catch (java.lang.reflect.InvocationTargetException e) {
|
113 |
//This could happen for various reasons if there is a problem in createAndShowGUI
|
114 |
e.printStackTrace(); |
115 |
} |
116 |
} |
117 |
|
118 |
public void destroy () { |
119 |
if (csi != null) { |
120 |
csi.disconnect(); |
121 |
} |
122 |
} |
123 |
|
124 |
private synchronized void createAndShowGUI () { |
125 |
// Webcam
|
126 |
gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
|
127 |
panelWebcam = new WebcamPanel();
|
128 |
tabPaneMain = new JTabbedPane(); |
129 |
tabPaneMain.setFont(new Font("arial", Font.PLAIN, 16)); |
130 |
tabPaneMain.add(panelWebcam, "Webcam");
|
131 |
|
132 |
// Robots
|
133 |
selectedBot = -1;
|
134 |
robotIcons = new RobotList();
|
135 |
|
136 |
// Connection area
|
137 |
txtInfo = new JTextArea(); |
138 |
txtInfo.setBorder(BorderFactory.createTitledBorder("Info")); |
139 |
txtHost = new JTextField(this.getDocumentBase().getHost()); |
140 |
txtHost.setBorder(BorderFactory.createTitledBorder("Host")); |
141 |
txtPort = new JTextField("10123"); |
142 |
txtPort.setBorder(BorderFactory.createTitledBorder("Port")); |
143 |
btnConnect = new JButton("Connect"); |
144 |
btnConnect.setFont(new Font("arial", Font.BOLD, 16)); |
145 |
btnGetXBeeIDs = new JButton("Get XBee IDs"); |
146 |
getRootPane().setDefaultButton(btnConnect); |
147 |
lblConnectionStatus = new JLabel("Status: Offline"); |
148 |
panelConnect = new JPanel(); |
149 |
panelConnect.setLayout(new GridLayout(6,1)); |
150 |
panelConnect.add(lblConnectionStatus); |
151 |
panelConnect.add(txtHost); |
152 |
panelConnect.add(txtPort); |
153 |
panelConnect.add(btnConnect); |
154 |
panelServerInterface = new JPanel(); |
155 |
panelServerInterface.setLayout(new GridLayout(2,1)); |
156 |
panelServerInterface.add(panelConnect); |
157 |
panelServerInterface.add(txtInfo); |
158 |
|
159 |
// Robot direction panel
|
160 |
panelRobotDirection = new JPanel(); |
161 |
panelRobotDirectionButtons = new JPanel(); |
162 |
|
163 |
Font f = new Font(null, Font.PLAIN, 18); |
164 |
btnF = new JButton("\u2191"); |
165 |
btnF.setFont(f); |
166 |
btnB = new JButton("\u2193"); |
167 |
btnB.setFont(f); |
168 |
btnL = new JButton("\u2190"); |
169 |
btnL.setFont(f); |
170 |
btnR = new JButton("\u2192"); |
171 |
btnR.setFont(f); |
172 |
btnActivate = new JButton("\u25A0"); |
173 |
btnActivate.setFont(new Font(null, Font.BOLD, 36)); |
174 |
|
175 |
panelRobotDirectionButtons.setLayout(new GridLayout(1,5)); |
176 |
panelRobotDirectionButtons.add(btnActivate); |
177 |
panelRobotDirectionButtons.add(btnF); |
178 |
panelRobotDirectionButtons.add(btnB); |
179 |
panelRobotDirectionButtons.add(btnL); |
180 |
panelRobotDirectionButtons.add(btnR); |
181 |
|
182 |
imageVectorControl = gc.createCompatibleImage(ColonetConstants.VECTOR_CONTROLLER_WIDTH, ColonetConstants.VECTOR_CONTROLLER_HEIGHT); |
183 |
vectorController = new VectorController(imageVectorControl, self);
|
184 |
panelRobotDirection.setLayout(new BorderLayout()); |
185 |
panelRobotDirection.add(vectorController, BorderLayout.CENTER);
|
186 |
panelRobotDirection.add(panelRobotDirectionButtons, BorderLayout.SOUTH);
|
187 |
|
188 |
// Robot Control and Commands
|
189 |
panelRobotCommands = new JPanel(); |
190 |
panelRobotCommands.setLayout(new GridLayout(5,2)); |
191 |
// Battery subset
|
192 |
batteryIcon = new BatteryIcon(0); |
193 |
lblBattery = new JLabel(batteryIcon); |
194 |
lblSelected = new JLabel("None"); |
195 |
// Management subset
|
196 |
setStation = false;
|
197 |
setWaypoint = false;
|
198 |
setWaypointID = -1;
|
199 |
btnAssignID = new JButton("Assign ID"); |
200 |
btnLocateStation = new JButton("Locate Charge Station"); |
201 |
boundary = new RobotBoundary();
|
202 |
btnSetBounds = new JButton("Set Boundary"); |
203 |
btnClearBounds = new JButton("Clear Boundary"); |
204 |
// Control subset
|
205 |
btnCommand_MoveTo = new JButton("Move to ..."); |
206 |
btnCommand_MoveAll = new JButton("Move all ..."); |
207 |
btnCommand_StopTask = new JButton("Stop Current Task"); |
208 |
btnCommand_ResumeTask = new JButton("Resume Current Task"); |
209 |
btnCommand_ChargeNow = new JButton("Recharge Now"); |
210 |
btnCommand_StopCharging = new JButton("Stop Recharging"); |
211 |
panelRobotCommands.add(new JLabel("Battery Level: ")); |
212 |
panelRobotCommands.add(lblBattery); |
213 |
panelRobotCommands.add(new JLabel("Selected Icon: ")); |
214 |
panelRobotCommands.add(lblSelected); |
215 |
panelRobotCommands.add(btnAssignID); |
216 |
panelRobotCommands.add(btnLocateStation); |
217 |
panelRobotCommands.add(btnSetBounds); |
218 |
panelRobotCommands.add(btnClearBounds); |
219 |
panelRobotCommands.add(btnCommand_MoveTo); |
220 |
panelRobotCommands.add(btnCommand_MoveAll); |
221 |
//panelRobotCommands.add(btnCommand_StopTask);
|
222 |
//panelRobotCommands.add(btnCommand_ResumeTask);
|
223 |
//panelRobotCommands.add(btnCommand_ChargeNow);
|
224 |
//panelRobotCommands.add(btnCommand_StopCharging);
|
225 |
panelRobotControl = new JPanel(); |
226 |
panelRobotControl.setLayout(new GridLayout(2,1)); |
227 |
panelRobotControl.add(panelRobotDirection); |
228 |
panelRobotControl.add(panelRobotCommands); |
229 |
|
230 |
// Task Manager
|
231 |
panelTaskManager = new JPanel(); |
232 |
panelTaskManager.setLayout(new BorderLayout()); |
233 |
taskListModel = new DefaultListModel(); |
234 |
taskList = new JList(taskListModel); |
235 |
taskList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
236 |
taskList.setSelectedIndex(0);
|
237 |
spTaskManager = new JScrollPane(taskList); |
238 |
panelTaskManagerControls = new JPanel(); |
239 |
panelTaskManagerControls.setLayout(new GridLayout(1,4)); |
240 |
panelTaskManagerControlsPriority = new JPanel(); |
241 |
panelTaskManagerControlsPriority.setLayout(new GridLayout(1,2)); |
242 |
btnAddTask = new JButton("Add..."); |
243 |
btnRemoveTask = new JButton("Remove"); |
244 |
btnMoveTaskUp = new JButton("^"); |
245 |
btnMoveTaskDown = new JButton("v"); |
246 |
btnUpdateTasks = new JButton("Update"); |
247 |
panelTaskManagerControlsPriority.add(btnMoveTaskUp); |
248 |
panelTaskManagerControlsPriority.add(btnMoveTaskDown); |
249 |
panelTaskManagerControls.add(btnAddTask); |
250 |
panelTaskManagerControls.add(btnRemoveTask); |
251 |
panelTaskManagerControls.add(btnUpdateTasks); |
252 |
panelTaskManagerControls.add(panelTaskManagerControlsPriority); |
253 |
panelTaskManager.add(spTaskManager, BorderLayout.CENTER);
|
254 |
panelTaskManager.add(panelTaskManagerControls, BorderLayout.SOUTH);
|
255 |
panelTaskManager.add(new JLabel("Current Task Queue"), BorderLayout.NORTH); |
256 |
taskAddWindow = new TaskAddWindow();
|
257 |
|
258 |
// Main control mechanism
|
259 |
tabPaneControl = new JTabbedPane(JTabbedPane.TOP); |
260 |
tabPaneControl.setFont(new Font("arial", Font.PLAIN, 16)); |
261 |
tabPaneControl.setPreferredSize(new Dimension(ColonetConstants.VECTOR_CONTROLLER_WIDTH, 0)); |
262 |
tabPaneControl.addTab("Connection", panelServerInterface);
|
263 |
tabPaneControl.addTab("Robots", panelRobotControl);
|
264 |
//tabPaneControl.addTab("Tasks", panelTaskManager);
|
265 |
|
266 |
// Put all elements in the ContentPane
|
267 |
this.getContentPane().setLayout(new BorderLayout()); |
268 |
this.getContentPane().add(tabPaneMain, BorderLayout.CENTER); |
269 |
this.getContentPane().add(tabPaneControl, BorderLayout.EAST); |
270 |
this.setVisible(true); |
271 |
|
272 |
// Disable components before connecting
|
273 |
btnConnect.setText("Connect");
|
274 |
lblConnectionStatus.setText("Status: Disconnected");
|
275 |
setComponentsEnabled(false);
|
276 |
|
277 |
/* Add all listeners here */
|
278 |
// Task Management
|
279 |
btnAddTask.addActionListener(this);
|
280 |
btnRemoveTask.addActionListener(this);
|
281 |
btnMoveTaskUp.addActionListener(this);
|
282 |
btnMoveTaskDown.addActionListener(this);
|
283 |
btnUpdateTasks.addActionListener(this);
|
284 |
// Robot Control
|
285 |
btnF.addActionListener(this);
|
286 |
btnB.addActionListener(this);
|
287 |
btnL.addActionListener(this);
|
288 |
btnR.addActionListener(this);
|
289 |
btnF.addKeyListener(this);
|
290 |
btnB.addKeyListener(this);
|
291 |
btnL.addKeyListener(this);
|
292 |
btnR.addKeyListener(this);
|
293 |
btnActivate.addActionListener(this);
|
294 |
btnActivate.addKeyListener(this);
|
295 |
btnCommand_MoveTo.addActionListener(this);
|
296 |
btnCommand_MoveAll.addActionListener(this);
|
297 |
btnCommand_StopTask.addActionListener(this);
|
298 |
btnCommand_ResumeTask.addActionListener(this);
|
299 |
btnCommand_ChargeNow.addActionListener(this);
|
300 |
btnCommand_StopCharging.addActionListener(this);
|
301 |
// Other
|
302 |
btnConnect.addActionListener(this);
|
303 |
btnGetXBeeIDs.addActionListener(this);
|
304 |
btnAssignID.addActionListener(this);
|
305 |
btnLocateStation.addActionListener(this);
|
306 |
btnSetBounds.addActionListener(this);
|
307 |
btnClearBounds.addActionListener(this);
|
308 |
panelWebcam.addMouseListener(this);
|
309 |
panelWebcam.addMouseMotionListener(this);
|
310 |
} |
311 |
|
312 |
public ColonetServerInterface getCSI () {
|
313 |
return csi;
|
314 |
} |
315 |
|
316 |
public void paint (Graphics g) { |
317 |
super.paint(g);
|
318 |
} |
319 |
|
320 |
public void update (Graphics g) { |
321 |
paint(g); |
322 |
} |
323 |
|
324 |
/**
|
325 |
* Gets the JTextArea used for displaying debugging information.
|
326 |
*
|
327 |
* @return the JTextArea where debugging information can be displayed.
|
328 |
*/
|
329 |
public JTextArea getInfoPanel () { |
330 |
return txtInfo;
|
331 |
} |
332 |
|
333 |
/**
|
334 |
* Parses a String containing BOM matrix information.
|
335 |
* The ColonetServerInterface receives lines of the BOM matrix. (For encoding
|
336 |
* information, see the ColonetServerInterface documentation.) The entire matrix is passed
|
337 |
* to the client when requested. This method takes a string of the form
|
338 |
* "[command code] [command code] [number of robots] [data0] [data1] ..."
|
339 |
* with tokens separated by spaces and containing no brackets.
|
340 |
* The [command code]s are predefined values identifying this String as a BOM data
|
341 |
* String, [number of robots] is an integer, and the values that follow are
|
342 |
* the sensor readings of the robots in order, starting with robot 0. Only [number of robots]^2
|
343 |
* data entries will be read. The matrix values are saved locally until the next String is parsed.
|
344 |
*
|
345 |
*
|
346 |
* @param line the String containing BOM matrix information.
|
347 |
* @throws ArrayIndexOutOfBoundsException if there are fewer than [number of robots]^2 data entries in the String
|
348 |
*/
|
349 |
public void parseMatrix (String line) { |
350 |
txtInfo.setText("");
|
351 |
String [] str = line.split(" "); |
352 |
int num = Integer.parseInt(str[2]); |
353 |
for (int i = 0; i < num; i++) { |
354 |
for (int j = 0; j < num; j++) { |
355 |
String next = str[3 + i*num + j]; |
356 |
if (next.equals("-1")) { |
357 |
txtInfo.append("-");
|
358 |
} else {
|
359 |
txtInfo.append(next); |
360 |
} |
361 |
|
362 |
if (j < num - 1) { |
363 |
txtInfo.append(" ");
|
364 |
} |
365 |
} |
366 |
|
367 |
if (i < num - 1) { |
368 |
txtInfo.append("\n");
|
369 |
} |
370 |
} |
371 |
repaint(); |
372 |
} |
373 |
|
374 |
public void connect () { |
375 |
if (csi != null) |
376 |
return;
|
377 |
csi = new ColonetServerInterface(self);
|
378 |
csi.connect(txtHost.getText(), txtPort.getText()); |
379 |
if (!csi.isReady()) {
|
380 |
csi = null;
|
381 |
return;
|
382 |
} |
383 |
webcamLoader = new WebcamLoader(self);
|
384 |
dataUpdater = new DataUpdater();
|
385 |
dataUpdater.start(); |
386 |
webcamLoader.start(); |
387 |
Runnable r = new Runnable() { |
388 |
public void run () { |
389 |
btnConnect.setText("Disconnect");
|
390 |
lblConnectionStatus.setText("Status: Connected");
|
391 |
setComponentsEnabled(true);
|
392 |
} |
393 |
}; |
394 |
SwingUtilities.invokeLater(r);
|
395 |
} |
396 |
|
397 |
public void disconnect () { |
398 |
try {
|
399 |
dataUpdater.interrupt(); |
400 |
} catch (Exception e) { |
401 |
} |
402 |
csi = null;
|
403 |
Runnable r = new Runnable() { |
404 |
public void run () { |
405 |
btnConnect.setText("Connect");
|
406 |
lblConnectionStatus.setText("Status: Disconnected");
|
407 |
setComponentsEnabled(false);
|
408 |
} |
409 |
}; |
410 |
SwingUtilities.invokeLater(r);
|
411 |
} |
412 |
|
413 |
private void setComponentsEnabled (boolean enabled) { |
414 |
btnF.setEnabled(enabled); |
415 |
btnB.setEnabled(enabled); |
416 |
btnL.setEnabled(enabled); |
417 |
btnR.setEnabled(enabled); |
418 |
btnActivate.setEnabled(enabled); |
419 |
btnAssignID.setEnabled(enabled); |
420 |
btnLocateStation.setEnabled(enabled); |
421 |
btnSetBounds.setEnabled(enabled); |
422 |
btnClearBounds.setEnabled(enabled); |
423 |
btnCommand_MoveTo.setEnabled(enabled); |
424 |
btnCommand_MoveAll.setEnabled(enabled); |
425 |
} |
426 |
|
427 |
/**
|
428 |
* Parses a String containing a task queue update.
|
429 |
* Format is currently not specified.
|
430 |
* This method currently does nothing.
|
431 |
*
|
432 |
* @param line the String containing task queue update information.
|
433 |
*/
|
434 |
public void parseQueue (String line) { |
435 |
|
436 |
} |
437 |
|
438 |
/**
|
439 |
* Parses a String containing XBee ID values.
|
440 |
* The ColonetServerInterface receives Strings of XBee information. (For encoding
|
441 |
* information, see the ColonetServerInterface documentation.) This method takes
|
442 |
* a string of the form "[command code] [command code] [number of robots] [id0] [id1] ..."
|
443 |
* with tokens separated by spaces and containing no brackets.
|
444 |
* The [command code]s are predefined values identifying this String as an XBee
|
445 |
* ID String, [number of robots] is an integer, and the values that follow are
|
446 |
* the IDs of the robots in order, starting with robot 0. Only [number of robots]
|
447 |
* will be read. The ID values are saved locally until the next String is parsed.
|
448 |
* The purpose of having this list is to ensure that robots are properly identified for control purposes.
|
449 |
* This keeps robot identification consistent between sessions and prevents arbitrary assignment.
|
450 |
*
|
451 |
* @param line the String containing XBee ID information.
|
452 |
* @throws ArrayIndexOutOfBoundsException if there are fewer than [number of robots] IDs in the String
|
453 |
* @see ColonetServerInterface#sendXBeeIDRequest()
|
454 |
*/
|
455 |
public void parseXBeeIDs (String line) { |
456 |
String [] str = line.split(" "); |
457 |
int num = Integer.parseInt(str[2]); |
458 |
xbeeID = new int[num]; |
459 |
for (int i = 0; i < num; i++) { |
460 |
xbeeID[i] = Integer.parseInt(str[i+3]); |
461 |
} |
462 |
} |
463 |
|
464 |
/**
|
465 |
* Parses a String containing battery information.
|
466 |
* The ColonetServerInterface receives Strings of battery information. (For encoding
|
467 |
* information, see the ColonetServerInterface documentation.) This method takes
|
468 |
* a string of the form "[command code] [command code] [robot ID] [value]"
|
469 |
* with tokens separated by spaces and containing no brackets.
|
470 |
* The [command code]s are predefined values identifying this String as a battery
|
471 |
* information String, [robot ID] is an integer, and [value] is a battery measurement.
|
472 |
* This updates the batery information for a single robot.
|
473 |
*
|
474 |
*
|
475 |
* @param line the String containing battery information.
|
476 |
* @see ColonetServerInterface#sendBatteryRequest(int)
|
477 |
*/
|
478 |
public void parseBattery (String line) { |
479 |
int botNum, level;
|
480 |
try {
|
481 |
String [] str = line.split(" "); |
482 |
botNum = Integer.parseInt(str[2]); |
483 |
level = Integer.parseInt(str[3]); |
484 |
} catch (NumberFormatException e) { |
485 |
System.out.println("Could not parse battery update"); |
486 |
return;
|
487 |
} |
488 |
|
489 |
RobotIcon r = robotIcons.get(botNum); |
490 |
if (r != null) { |
491 |
r.battery = batteryIcon.convert(level); // set contextual battery meter
|
492 |
batteryIcon.setLevel(level); // set solo battery meter
|
493 |
} |
494 |
super.repaint();
|
495 |
} |
496 |
|
497 |
/**
|
498 |
* Parses a String containing visual robot position information along with
|
499 |
* canonical ID assignments.
|
500 |
*/
|
501 |
public void parsePositions (String line) { |
502 |
String [] str = line.split(" "); |
503 |
RobotList newList = new RobotList();
|
504 |
|
505 |
for (int i = 2; i < str.length; i+=3) { |
506 |
int id = Integer.parseInt(str[i]); |
507 |
int x = Integer.parseInt(str[i+1]); |
508 |
int y = Integer.parseInt(str[i+2]); |
509 |
RobotIcon newIcon = new RobotIcon(id, x, y);
|
510 |
// Save previous robot information
|
511 |
RobotIcon oldIcon = robotIcons.get(id); |
512 |
if (oldIcon != null) { |
513 |
newIcon.battery = oldIcon.battery; |
514 |
newIcon.destx = oldIcon.destx; |
515 |
newIcon.desty = oldIcon.desty; |
516 |
} |
517 |
if (newIcon.id >= 0) { |
518 |
newIcon.color = Color.GREEN;
|
519 |
} |
520 |
newList.put(id, newIcon); |
521 |
} |
522 |
robotIcons = newList; |
523 |
repaint(); |
524 |
} |
525 |
|
526 |
/**
|
527 |
* Parses a message that indicates a robot has reached its destination
|
528 |
*/
|
529 |
public void parseMoveUpdate (String line) { |
530 |
System.out.println("Got move update: " + line); |
531 |
String [] str = line.split(" "); |
532 |
int id = Integer.parseInt(str[1]); |
533 |
robotIcons.cancelMoveTo(id); |
534 |
} |
535 |
|
536 |
/**
|
537 |
* Set the ID of the selected robot
|
538 |
*/
|
539 |
public void setSelectedBot (int id) { |
540 |
this.selectedBot = id;
|
541 |
} |
542 |
|
543 |
/**
|
544 |
* Returns the ID of the selected robot
|
545 |
*/
|
546 |
public int getSelectedBot () { |
547 |
return this.selectedBot; |
548 |
} |
549 |
|
550 |
//
|
551 |
// MouseListener methods
|
552 |
//
|
553 |
public void mousePressed(MouseEvent e) { |
554 |
//Start a new Thread to handle the MouseEvent
|
555 |
(new MouseHandler(e)).start();
|
556 |
} |
557 |
public void mouseExited(MouseEvent e) { |
558 |
} |
559 |
public void mouseEntered(MouseEvent e) { |
560 |
} |
561 |
public void mouseReleased(MouseEvent e) { |
562 |
(new MouseHandler(e)).start();
|
563 |
} |
564 |
public void mouseClicked(MouseEvent e) { |
565 |
} |
566 |
public void mouseDragged(MouseEvent e) { |
567 |
(new MouseHandler(e)).start();
|
568 |
} |
569 |
public void mouseMoved(MouseEvent e) { |
570 |
} |
571 |
|
572 |
//
|
573 |
// KeyListener methods
|
574 |
//
|
575 |
public void keyPressed (KeyEvent e) { |
576 |
//Start a new Thread to handle the KeyEvent
|
577 |
(new KeyHandler(e)).start();
|
578 |
repaint(); |
579 |
} |
580 |
public void keyReleased (KeyEvent e) { |
581 |
} |
582 |
public void keyTyped (KeyEvent e) { |
583 |
} |
584 |
|
585 |
//
|
586 |
// ActionListener method
|
587 |
//
|
588 |
public void actionPerformed (ActionEvent e) { |
589 |
// Start a new Thread to handle the ActionEvent
|
590 |
(new ActionHandler(e)).start();
|
591 |
repaint(); |
592 |
} |
593 |
|
594 |
class MouseHandler extends Thread { |
595 |
MouseEvent e;
|
596 |
|
597 |
public MouseHandler (MouseEvent event) { |
598 |
super("MouseHandler"); |
599 |
this.e = event;
|
600 |
} |
601 |
|
602 |
public void run () { |
603 |
Point pt = panelWebcam.convertClick(e);
|
604 |
|
605 |
// If we are selecting a waypoint (destination) for a specific bot
|
606 |
if (setWaypoint && setWaypointID >= 0 && e.getID() == MouseEvent.MOUSE_PRESSED) { |
607 |
// If the user clicks outside the boundary, do nothing
|
608 |
if (boundary.isActive() && !boundary.contains(e.getX(), e.getY())) {
|
609 |
return;
|
610 |
} |
611 |
setWaypoint = false;
|
612 |
panelWebcam.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); |
613 |
if (selectedBot < 0) { |
614 |
return;
|
615 |
} |
616 |
|
617 |
RobotIcon r = robotIcons.get(selectedBot); |
618 |
if (r != null) { |
619 |
r.destx = pt.x; |
620 |
r.desty = pt.y; |
621 |
if (csi != null) { |
622 |
csi.sendAbsoluteMove(r.id, r.destx, r.desty); |
623 |
} |
624 |
} |
625 |
return;
|
626 |
} |
627 |
|
628 |
// If we are ordering a charge (right-click on a station)
|
629 |
if ((e.getButton() == MouseEvent.BUTTON2 || e.getButton() == MouseEvent.BUTTON3) |
630 |
&& e.getID() == MouseEvent.MOUSE_PRESSED
|
631 |
&& station != null && station.contains(e.getX(), e.getY())) {
|
632 |
//TODO: send charge command
|
633 |
return;
|
634 |
} |
635 |
|
636 |
// Right-click also means we are moving a robot
|
637 |
if ((e.getButton() == MouseEvent.BUTTON2 || e.getButton() == MouseEvent.BUTTON3) |
638 |
&& e.getID() == MouseEvent.MOUSE_PRESSED) {
|
639 |
// If the user clicks outside the boundary, do nothing
|
640 |
if (boundary.isActive() && !boundary.contains(e.getX(), e.getY())) {
|
641 |
return;
|
642 |
} |
643 |
if (selectedBot < 0) { |
644 |
return;
|
645 |
} |
646 |
|
647 |
RobotIcon r = robotIcons.get(selectedBot); |
648 |
if (r != null) { |
649 |
r.destx = pt.x; |
650 |
r.desty = pt.y; |
651 |
if (csi != null) { |
652 |
csi.sendAbsoluteMove(r.id, r.destx, r.desty); |
653 |
} |
654 |
} |
655 |
return;
|
656 |
} |
657 |
|
658 |
// If we are setting all waypoints
|
659 |
if (setWaypoint && e.getID() == MouseEvent.MOUSE_PRESSED) { |
660 |
// If the user clicks outside the boundary, do nothing
|
661 |
if (boundary.isActive() && !boundary.contains(e.getX(), e.getY())) {
|
662 |
return;
|
663 |
} |
664 |
setWaypoint = false;
|
665 |
panelWebcam.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); |
666 |
for (Map.Entry<Integer,RobotIcon> entry : robotIcons.entrySet()) { |
667 |
RobotIcon r = entry.getValue(); |
668 |
r.destx = pt.x; |
669 |
r.desty = pt.y; |
670 |
} |
671 |
return;
|
672 |
} |
673 |
|
674 |
// If we are drawing a boundary rectangle
|
675 |
if (boundary != null && boundary.isSetting()) { |
676 |
// Begin
|
677 |
if (e.getID() == MouseEvent.MOUSE_PRESSED) { |
678 |
boundary.panel_p1 = new Point(e.getX(), e.getY()); |
679 |
boundary.panel_p2 = new Point(e.getX(), e.getY()); |
680 |
boundary.img_p1 = new Point(pt.x, pt.y); |
681 |
} |
682 |
// Resize
|
683 |
else if (e.getID() == MouseEvent.MOUSE_DRAGGED){ |
684 |
boundary.panel_p2 = new Point(e.getX(), e.getY()); |
685 |
} |
686 |
// Finish
|
687 |
else if (e.getID() == MouseEvent.MOUSE_RELEASED){ |
688 |
boundary.panel_p2 = new Point(e.getX(), e.getY()); |
689 |
boundary.img_p2 = new Point(pt.x, pt.y); |
690 |
panelWebcam.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); |
691 |
boundary.set = false;
|
692 |
boundary.active = true;
|
693 |
boundary.sendToServer(); |
694 |
} |
695 |
return;
|
696 |
} |
697 |
|
698 |
// If we are locating a charging station
|
699 |
if (setStation) {
|
700 |
setStation = false;
|
701 |
station = new ChargingStation(e.getX(), e.getY());
|
702 |
station.setActive(true);
|
703 |
return;
|
704 |
} |
705 |
|
706 |
// Otherwise, we are selecting a bot, or doing nothing
|
707 |
RobotIcon r = robotIcons.getBoundingIcon(pt); |
708 |
if (r != null) |
709 |
selectedBot = r.id; |
710 |
} |
711 |
} |
712 |
|
713 |
class KeyHandler extends Thread { |
714 |
KeyEvent e;
|
715 |
|
716 |
public KeyHandler (KeyEvent event) { |
717 |
super("KeyHandler"); |
718 |
this.e = event;
|
719 |
} |
720 |
|
721 |
public void run () { |
722 |
int code = e.getKeyCode();
|
723 |
if (code == KeyEvent.VK_UP) { |
724 |
vectorController.setMaxForward(); |
725 |
vectorController.sendToServer(); |
726 |
} else if (code == KeyEvent.VK_DOWN) { |
727 |
vectorController.setMaxReverse(); |
728 |
vectorController.sendToServer(); |
729 |
} else if (code == KeyEvent.VK_LEFT) { |
730 |
vectorController.setMaxLeft(); |
731 |
vectorController.sendToServer(); |
732 |
} else if (code == KeyEvent.VK_RIGHT) { |
733 |
vectorController.setMaxRight(); |
734 |
vectorController.sendToServer(); |
735 |
} else if (code == KeyEvent.VK_S) { |
736 |
vectorController.setZero(); |
737 |
vectorController.sendToServer(); |
738 |
} |
739 |
} |
740 |
} |
741 |
|
742 |
class ActionHandler extends Thread { |
743 |
ActionEvent e;
|
744 |
|
745 |
public ActionHandler (ActionEvent event) { |
746 |
super("ActionHandler"); |
747 |
this.e = event;
|
748 |
} |
749 |
|
750 |
public void run () { |
751 |
Object source = e.getSource();
|
752 |
|
753 |
// General Actions
|
754 |
if (source == btnConnect) {
|
755 |
if (csi == null) { |
756 |
connect(); |
757 |
} else {
|
758 |
disconnect(); |
759 |
} |
760 |
} else if (source == btnGetXBeeIDs) { |
761 |
csi.sendXBeeIDRequest(); |
762 |
} else if (source == btnAssignID) { |
763 |
String message;
|
764 |
if (selectedBot < 0) { |
765 |
message = "That robot is unidentified. Please specify its ID.";
|
766 |
} else {
|
767 |
message = "That robot has ID " + selectedBot + ". You may reassign it now."; |
768 |
} |
769 |
String result = JOptionPane.showInputDialog(self, message, "Robot Identification", JOptionPane.QUESTION_MESSAGE); |
770 |
if (result == null) { |
771 |
return;
|
772 |
} |
773 |
int newID = -1; |
774 |
try {
|
775 |
newID = Integer.parseInt(result);
|
776 |
} catch (Exception ex) { |
777 |
csi.warn("Invalid ID.");
|
778 |
return;
|
779 |
} |
780 |
// Assign new ID and update display
|
781 |
if (csi != null) { |
782 |
csi.sendIDAssignment(selectedBot, newID); |
783 |
} |
784 |
selectedBot = newID; |
785 |
lblSelected.setText("" + newID);
|
786 |
} else if (source == btnLocateStation) { |
787 |
setStation = true;
|
788 |
} else if (source == btnF) { // Robot Movement Controls |
789 |
vectorController.setMaxForward(); |
790 |
vectorController.sendToServer(); |
791 |
robotIcons.cancelMoveTo(selectedBot); |
792 |
} else if (source == btnB) { |
793 |
vectorController.setMaxReverse(); |
794 |
vectorController.sendToServer(); |
795 |
robotIcons.cancelMoveTo(selectedBot); |
796 |
} else if (source == btnL) { |
797 |
vectorController.setMaxLeft(); |
798 |
vectorController.sendToServer(); |
799 |
robotIcons.cancelMoveTo(selectedBot); |
800 |
} else if (source == btnR) { |
801 |
vectorController.setMaxRight(); |
802 |
vectorController.sendToServer(); |
803 |
robotIcons.cancelMoveTo(selectedBot); |
804 |
} else if (source == btnActivate) { |
805 |
vectorController.setZero(); |
806 |
vectorController.sendToServer(); |
807 |
robotIcons.cancelMoveTo(selectedBot); |
808 |
} else if (source == btnSetBounds) { |
809 |
boundary.set = true;
|
810 |
panelWebcam.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); |
811 |
} else if (source == btnClearBounds) { |
812 |
boundary.active = false;
|
813 |
boundary.set = false;
|
814 |
boundary.img_p1 = new Point(-1, -1); |
815 |
boundary.img_p2 = new Point(-1, -1); |
816 |
boundary.panel_p1 = new Point(-1, -1); |
817 |
boundary.panel_p2 = new Point(-1, -1); |
818 |
boundary.sendToServer(); |
819 |
} else if (source == btnCommand_MoveTo) { |
820 |
if (selectedBot < 0) { |
821 |
return;
|
822 |
} |
823 |
panelWebcam.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); |
824 |
setWaypoint = true;
|
825 |
setWaypointID = selectedBot; |
826 |
} else if (source == btnCommand_MoveAll) { |
827 |
panelWebcam.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR)); |
828 |
setWaypoint = true;
|
829 |
setWaypointID = -1;
|
830 |
} else if (source == btnCommand_StopTask) { |
831 |
|
832 |
} else if (source == btnCommand_ResumeTask) { |
833 |
|
834 |
} else if (source == btnCommand_ChargeNow) { |
835 |
|
836 |
} else if (source == btnCommand_StopCharging) { |
837 |
|
838 |
} else if (source == btnAddTask) { // Queue Management |
839 |
taskAddWindow.prompt(); |
840 |
} else if (source == btnRemoveTask) { |
841 |
if (taskList.getSelectedIndex() >= 0) { |
842 |
csi.sendQueueRemove(taskList.getSelectedIndex()); |
843 |
} |
844 |
csi.sendQueueUpdate(); |
845 |
} else if (source == btnMoveTaskUp) { |
846 |
csi.sendQueueReorder(taskList.getSelectedIndex(), taskList.getSelectedIndex() - 1);
|
847 |
csi.sendQueueUpdate(); |
848 |
} else if (source == btnMoveTaskDown) { |
849 |
csi.sendQueueReorder(taskList.getSelectedIndex(), taskList.getSelectedIndex() + 1);
|
850 |
csi.sendQueueUpdate(); |
851 |
} else if (source == btnUpdateTasks) { |
852 |
csi.sendQueueUpdate(); |
853 |
} |
854 |
|
855 |
} |
856 |
} |
857 |
|
858 |
/*
|
859 |
* DataUpdater thread.
|
860 |
* The purpose of this thread is to request data from the server at regular intervals.
|
861 |
* Types of data requested: XBee IDs, robot positions, battery levels.
|
862 |
*
|
863 |
*/
|
864 |
class DataUpdater extends Thread { |
865 |
final int DATAUPDATER_DELAY = 200; |
866 |
int count;
|
867 |
public DataUpdater () {
|
868 |
super("Colonet DataUpdater"); |
869 |
count = 0;
|
870 |
} |
871 |
|
872 |
public synchronized void run () { |
873 |
while (true) { |
874 |
try {
|
875 |
if (csi != null && csi.isReady()) { |
876 |
// XBee ID Request
|
877 |
if (count % 5 == 0) { |
878 |
csi.sendXBeeIDRequest(); |
879 |
} |
880 |
// Robot Position Request
|
881 |
if (count % 1 == 0) { |
882 |
csi.sendPositionRequest(); |
883 |
} |
884 |
// Battery Request
|
885 |
if (count % 30 == 0) { |
886 |
for (Map.Entry<Integer,RobotIcon> entry : robotIcons.entrySet()) { |
887 |
RobotIcon r = entry.getValue(); |
888 |
int id = r.id;
|
889 |
if (id >= 0) { |
890 |
csi.sendBatteryRequest(id); |
891 |
} |
892 |
} |
893 |
} |
894 |
} |
895 |
count++; |
896 |
if (count > 1000) |
897 |
count = 0;
|
898 |
Thread.sleep(DATAUPDATER_DELAY);
|
899 |
} catch (InterruptedException e) { |
900 |
return;
|
901 |
} catch (NullPointerException e) { |
902 |
// This probably indicates that an object was destroyed by shutdown.
|
903 |
return;
|
904 |
} |
905 |
} |
906 |
} |
907 |
} |
908 |
|
909 |
|
910 |
/*
|
911 |
* WebcamPanel class
|
912 |
* Contains the webcam image
|
913 |
*/
|
914 |
class WebcamPanel extends JPanel { |
915 |
|
916 |
volatile BufferedImage img; |
917 |
BufferedImage buffer;
|
918 |
|
919 |
public WebcamPanel () {
|
920 |
super(true); |
921 |
} |
922 |
|
923 |
public synchronized void setImage (BufferedImage newimg) { |
924 |
if (img != null) { |
925 |
img.flush(); |
926 |
} |
927 |
System.gc();
|
928 |
img = newimg; |
929 |
repaint(); |
930 |
} |
931 |
|
932 |
public synchronized void paint (Graphics g) { |
933 |
if (img == null) { |
934 |
return;
|
935 |
} |
936 |
|
937 |
// Calculate scaling
|
938 |
int maxWidth = getWidth() - 2*ColonetConstants.WEBCAM_BORDER; |
939 |
int maxHeight = getHeight() - 2*ColonetConstants.WEBCAM_BORDER; |
940 |
double widthRatio = 1.0 * maxWidth / img.getWidth(); |
941 |
double heightRatio = 1.0 * maxHeight / img.getHeight(); |
942 |
double scale = 0; |
943 |
int newWidth = 0; |
944 |
int newHeight = 0; |
945 |
int x = 0; |
946 |
int y = 0; |
947 |
|
948 |
if (widthRatio > heightRatio) { //height is the limiting factor |
949 |
scale = heightRatio; |
950 |
newHeight = maxHeight; |
951 |
newWidth = (int) (img.getWidth() * scale);
|
952 |
y = ColonetConstants.WEBCAM_BORDER; |
953 |
x = (maxWidth - newWidth) / 2 + ColonetConstants.WEBCAM_BORDER;
|
954 |
} else { //width is the limiting factor |
955 |
scale = widthRatio; |
956 |
newWidth = maxWidth; |
957 |
newHeight = (int) (img.getHeight() * scale);
|
958 |
x = ColonetConstants.WEBCAM_BORDER; |
959 |
y = (maxHeight - newHeight) / 2 + ColonetConstants.WEBCAM_BORDER;
|
960 |
} |
961 |
|
962 |
// Draw image onto the buffer
|
963 |
buffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB); |
964 |
Graphics2D bufferedGraphics = (Graphics2D)buffer.getGraphics(); |
965 |
bufferedGraphics.setColor(Color.GRAY);
|
966 |
bufferedGraphics.fillRect(0, 0, this.getWidth(), this.getHeight()); |
967 |
Image imgScaled = img.getScaledInstance(newWidth, newHeight, Image.SCALE_FAST); |
968 |
bufferedGraphics.drawImage(imgScaled, x, y, this);
|
969 |
|
970 |
// Draw boundary
|
971 |
if (boundary.isSetting() || boundary.isActive()) {
|
972 |
bufferedGraphics.setColor(Color.BLUE);
|
973 |
int width = boundary.panel_p2.x - boundary.panel_p1.x;
|
974 |
int height = boundary.panel_p2.y - boundary.panel_p1.y;
|
975 |
bufferedGraphics.drawRect(boundary.panel_p1.x, boundary.panel_p1.y, width, height); |
976 |
} |
977 |
|
978 |
// Draw charging station
|
979 |
if (station != null && station.isActive()) { |
980 |
bufferedGraphics.setStroke(new BasicStroke(2)); |
981 |
bufferedGraphics.setColor(Color.ORANGE);
|
982 |
int width = ColonetConstants.STATION_SIZE;
|
983 |
bufferedGraphics.drawRect(station.getX() - width/2, station.getY() - width/2, width, width); |
984 |
} |
985 |
|
986 |
// Draw Identifiers and battery levels
|
987 |
if (robotIcons != null) { |
988 |
bufferedGraphics.setStroke(new BasicStroke(2)); |
989 |
for (Map.Entry<Integer,RobotIcon> entry : robotIcons.entrySet()) { |
990 |
RobotIcon r = entry.getValue(); |
991 |
bufferedGraphics.setColor(r.color); |
992 |
// Identifier circle
|
993 |
int px = (int) (x + r.x * scale); |
994 |
int py = (int) (y + r.y * scale); |
995 |
int radius = ColonetConstants.ROBOT_RADIUS;
|
996 |
bufferedGraphics.drawOval(px-radius, py-radius, 2*radius, 2*radius); |
997 |
// ID, if applicable
|
998 |
if (r.id > 0) { |
999 |
bufferedGraphics.setFont(new Font("arial", Font.PLAIN, 36)); |
1000 |
bufferedGraphics.drawString("" + r.id, px-10, py+10); |
1001 |
} |
1002 |
// Battery
|
1003 |
if (r.battery >= 0) { |
1004 |
int pixels = r.battery * ColonetConstants.BATTERY_WIDTH / 100; |
1005 |
if (r.battery > 50) |
1006 |
bufferedGraphics.setColor(Color.GREEN);
|
1007 |
else if (r.battery > 25) |
1008 |
bufferedGraphics.setColor(Color.YELLOW);
|
1009 |
else
|
1010 |
bufferedGraphics.setColor(Color.RED);
|
1011 |
bufferedGraphics.fillRect(px+20, py+20, pixels, ColonetConstants.BATTERY_HEIGHT); |
1012 |
bufferedGraphics.setColor(Color.BLACK);
|
1013 |
bufferedGraphics.drawRect(px+20, py+20, ColonetConstants.BATTERY_WIDTH, ColonetConstants.BATTERY_HEIGHT); |
1014 |
} |
1015 |
// If the robot has a destination, draw the vector
|
1016 |
if (r.destx >= 0) { |
1017 |
bufferedGraphics.drawLine(px, py, (int)(x + r.destx * scale), (int)(y + r.desty * scale)); |
1018 |
} |
1019 |
} |
1020 |
} |
1021 |
|
1022 |
// Identify currently-selected robot
|
1023 |
RobotIcon r = robotIcons.get(selectedBot); |
1024 |
if (r != null) { |
1025 |
int px = (int) (x + r.x * scale); |
1026 |
int py = (int) (y + r.y * scale); |
1027 |
int radius = ColonetConstants.ROBOT_RADIUS;
|
1028 |
bufferedGraphics.setColor(Color.BLACK);
|
1029 |
bufferedGraphics.drawOval(px-radius-6, py-radius-6, 2*radius+12, 2*radius+12); |
1030 |
} |
1031 |
|
1032 |
//Display buffered content
|
1033 |
g.drawImage(buffer, 0, 0, this); |
1034 |
} |
1035 |
|
1036 |
/**
|
1037 |
* Convert a click on the webcam panel to a coordinate that is consistent with the
|
1038 |
* original size of the image that the panel contains.
|
1039 |
*/
|
1040 |
public Point convertClick (MouseEvent e) { |
1041 |
if (img == null) { |
1042 |
return new Point(e.getX(), e.getY()); |
1043 |
} |
1044 |
|
1045 |
// Calculate scaling
|
1046 |
int border = ColonetConstants.WEBCAM_BORDER;
|
1047 |
int clickx = e.getX();
|
1048 |
int clicky = e.getY();
|
1049 |
int maxWidth = getWidth() - 2*border; |
1050 |
int maxHeight = getHeight() - 2*border; |
1051 |
double widthRatio = 1.0 * maxWidth / img.getWidth(); |
1052 |
double heightRatio = 1.0 * maxHeight / img.getHeight(); |
1053 |
double scale = 0; |
1054 |
int newWidth = 0; |
1055 |
int newHeight = 0; |
1056 |
int px = 0; |
1057 |
int py = 0; |
1058 |
|
1059 |
if (widthRatio > heightRatio) { //height is the limiting factor |
1060 |
scale = heightRatio; |
1061 |
newHeight = maxHeight; |
1062 |
newWidth = (int) (img.getWidth() * scale);
|
1063 |
py = clicky - border; |
1064 |
px = clickx - border - (maxWidth - newWidth) / 2;
|
1065 |
} else { //width is the limiting factor |
1066 |
scale = widthRatio; |
1067 |
newWidth = maxWidth; |
1068 |
newHeight = (int) (img.getHeight() * scale);
|
1069 |
px = clickx - border; |
1070 |
py = clicky - border - (maxHeight - newHeight) / 2;
|
1071 |
} |
1072 |
py = (int) (py / scale);
|
1073 |
px = (int) (px / scale);
|
1074 |
return new Point(px, py); |
1075 |
} |
1076 |
|
1077 |
} |
1078 |
|
1079 |
/*
|
1080 |
* WebcamLoader class
|
1081 |
* Handles the loading of the webcam image.
|
1082 |
*/
|
1083 |
class WebcamLoader extends Thread |
1084 |
{ |
1085 |
URL imagePath;
|
1086 |
MediaTracker mt;
|
1087 |
BufferedImage image;
|
1088 |
Random rand;
|
1089 |
|
1090 |
public WebcamLoader (JApplet applet) |
1091 |
{ |
1092 |
super("ColonetWebcamLoader"); |
1093 |
mt = new MediaTracker(applet); |
1094 |
ImageIO.setUseCache(false); |
1095 |
rand = new Random(); |
1096 |
} |
1097 |
|
1098 |
public void run () |
1099 |
{ |
1100 |
while (true) { |
1101 |
try {
|
1102 |
Thread.sleep(ColonetConstants.WEBCAM_DELAY);
|
1103 |
if (image != null) |
1104 |
image.flush(); |
1105 |
System.gc();
|
1106 |
try {
|
1107 |
imagePath = new URL(ColonetConstants.WEBCAM_PATH + "?rand=" + rand.nextInt(100000)); |
1108 |
} catch (MalformedURLException e) { |
1109 |
System.out.println("Malformed URL: could not form URL from: [" + ColonetConstants.WEBCAM_PATH + "]\n"); |
1110 |
} |
1111 |
image = ImageIO.read(imagePath);
|
1112 |
// The MediaTracker waitForID pauses the thread until the image is loaded.
|
1113 |
// We don't want to display a half-downloaded image.
|
1114 |
mt.addImage(image, 1);
|
1115 |
while(!mt.checkID(1)) |
1116 |
Thread.sleep(20); |
1117 |
mt.removeImage(image); |
1118 |
// Save
|
1119 |
panelWebcam.setImage(image); |
1120 |
} catch (InterruptedException e) { |
1121 |
return;
|
1122 |
} catch (java.security.AccessControlException e) {
|
1123 |
csi.warn("Could not load webcam.\n" + e);
|
1124 |
return;
|
1125 |
} catch (IOException e) { |
1126 |
getInfoPanel().append("IOException while trying to load image.");
|
1127 |
} |
1128 |
} |
1129 |
} |
1130 |
} |
1131 |
|
1132 |
class RobotBoundary { |
1133 |
public volatile boolean active, set; |
1134 |
public volatile Point img_p1, img_p2; |
1135 |
public volatile Point panel_p1, panel_p2; |
1136 |
|
1137 |
public RobotBoundary () {
|
1138 |
active = false;
|
1139 |
set = false;
|
1140 |
img_p1 = new Point(-1,-1); |
1141 |
img_p2 = new Point(-1,-1); |
1142 |
panel_p1 = new Point(-1,-1); |
1143 |
panel_p2 = new Point(-1,-1); |
1144 |
} |
1145 |
|
1146 |
public void sendToServer () { |
1147 |
if (csi != null) { |
1148 |
if (img_p1.x == -1 || img_p1.y == -1) |
1149 |
csi.sendBoundaryClear(); |
1150 |
else
|
1151 |
csi.sendBoundary(img_p1.x, img_p1.y, img_p2.x, img_p2.y); |
1152 |
} |
1153 |
} |
1154 |
|
1155 |
public boolean isActive () { |
1156 |
return active;
|
1157 |
} |
1158 |
|
1159 |
public boolean isSetting () { |
1160 |
return set;
|
1161 |
} |
1162 |
|
1163 |
/**
|
1164 |
* Returns a Rectangle designating the active area
|
1165 |
* in the coordinate system of the JPanel.
|
1166 |
* Returns null if the boundary is not active.
|
1167 |
*/
|
1168 |
public Rectangle toRectangle () { |
1169 |
if (!active)
|
1170 |
return null; |
1171 |
return new Rectangle (panel_p1.x, panel_p1.y, (panel_p2.x-panel_p1.x), (panel_p2.y-panel_p1.y)); |
1172 |
} |
1173 |
|
1174 |
/**
|
1175 |
* Determines whether a coordinate in the coordinate
|
1176 |
* system of the JPanel is contained within the boundary.
|
1177 |
* If the boundary is not active, then this method
|
1178 |
* returns false.
|
1179 |
*/
|
1180 |
public boolean contains (int px, int py) { |
1181 |
Rectangle rect = this.toRectangle(); |
1182 |
if (rect == null || !this.active) |
1183 |
return false; |
1184 |
return rect.contains(px, py);
|
1185 |
} |
1186 |
|
1187 |
} |
1188 |
|
1189 |
class ChargingStation { |
1190 |
private int x, y; |
1191 |
private boolean active; |
1192 |
|
1193 |
public ChargingStation () {
|
1194 |
super();
|
1195 |
} |
1196 |
|
1197 |
public ChargingStation (int x, int y) { |
1198 |
this();
|
1199 |
setLocation(x, y); |
1200 |
} |
1201 |
|
1202 |
public int getX () { |
1203 |
return x;
|
1204 |
} |
1205 |
|
1206 |
public int getY () { |
1207 |
return y;
|
1208 |
} |
1209 |
|
1210 |
/**
|
1211 |
* Sets the center of the charging station to
|
1212 |
* the specified coordinate point.
|
1213 |
*/
|
1214 |
public void setLocation (int x, int y) { |
1215 |
this.x = x;
|
1216 |
this.y = y;
|
1217 |
} |
1218 |
|
1219 |
public boolean isActive () { |
1220 |
return active;
|
1221 |
} |
1222 |
|
1223 |
public void setActive (boolean status) { |
1224 |
this.active = status;
|
1225 |
} |
1226 |
|
1227 |
/**
|
1228 |
* Determines whether a coordinate is contained within
|
1229 |
* the graphical area of the charging station.
|
1230 |
* Returns false if the charging station is not active.
|
1231 |
*/
|
1232 |
public boolean contains (int px, int py) { |
1233 |
if (!active)
|
1234 |
return false; |
1235 |
int width = ColonetConstants.STATION_SIZE;
|
1236 |
Rectangle rect = new Rectangle(this.x - width/2, this.y - width/2, width, width); |
1237 |
return rect.contains(px, py);
|
1238 |
} |
1239 |
|
1240 |
} |
1241 |
|
1242 |
} |