root / trunk / code / projects / colonet / ColonetGUI / Colonet.java @ 181
History | View | Annotate | Download (35.3 KB)
1 |
//
|
---|---|
2 |
// Colonet.java
|
3 |
//
|
4 |
|
5 |
import javax.swing.*; |
6 |
import javax.swing.event.*; |
7 |
import java.awt.*; |
8 |
import java.awt.image.*; |
9 |
import java.awt.event.*; |
10 |
import java.net.*; |
11 |
import java.io.*; |
12 |
import java.util.Random; |
13 |
import java.applet.*; |
14 |
|
15 |
public class Colonet extends JApplet implements ActionListener, MouseInputListener, KeyListener, Runnable { |
16 |
|
17 |
final int CANVAS_SIZE = 500; //the applet may be slow if the canvas gets too large |
18 |
final int BUFFER = 50; |
19 |
final int RADIUS = 30; |
20 |
|
21 |
// Connection
|
22 |
JTextField txtHost;
|
23 |
JTextField txtPort;
|
24 |
JButton btnConnect;
|
25 |
JButton btnGraph;
|
26 |
JLabel lblConnectionStatus;
|
27 |
JTextArea txtMatrix;
|
28 |
JTextArea txtInfo;
|
29 |
JPanel panelConnect;
|
30 |
JPanel panelServerInterface;
|
31 |
|
32 |
// Stats
|
33 |
JLabel lblBattery;
|
34 |
JLabel lblTokenPasses;
|
35 |
JLabel lblHastToken;
|
36 |
JPanel panelStats;
|
37 |
|
38 |
// South
|
39 |
JPanel panelSouth;
|
40 |
JTextArea log;
|
41 |
JScrollPane spLog;
|
42 |
|
43 |
// Control
|
44 |
JPanel panelControl;
|
45 |
JTabbedPane tabPaneControl;
|
46 |
JPanel panelRobotControl;
|
47 |
JPanel panelRobotDirection;
|
48 |
JPanel panelRobotDirectionButtons;
|
49 |
JPanel panelRobotCommands;
|
50 |
JButton btnF, btnB, btnL, btnR, btnActivate;
|
51 |
JComboBox cmbRobotNum;
|
52 |
VectorController vectorController; |
53 |
BufferedImage imageVectorControl;
|
54 |
JButton btnCommand_StopTask;
|
55 |
JButton btnCommand_ResumeTask;
|
56 |
JButton btnCommand_ChargeNow;
|
57 |
JButton btnCommand_StopCharging;
|
58 |
|
59 |
// Task Manager
|
60 |
JPanel panelTaskManager;
|
61 |
JScrollPane spTaskManager;
|
62 |
JPanel panelTaskManagerControls;
|
63 |
JPanel panelTaskManagerControlsPriority;
|
64 |
DefaultListModel taskListModel;
|
65 |
JList taskList;
|
66 |
JButton btnAddTask;
|
67 |
JButton btnRemoveTask;
|
68 |
JButton btnMoveTaskUp;
|
69 |
JButton btnMoveTaskDown;
|
70 |
JButton btnUpdateTasks;
|
71 |
TaskAddWindow taskAddWindow; |
72 |
|
73 |
// Graphics
|
74 |
ImagePanel panel; |
75 |
GraphicsConfiguration gc;
|
76 |
volatile BufferedImage image; |
77 |
volatile Graphics2D canvas; |
78 |
int cx, cy;
|
79 |
|
80 |
Socket socket;
|
81 |
OutputStreamWriter out;
|
82 |
DataUpdater dataUpdater; |
83 |
|
84 |
Font botFont;
|
85 |
Random random = new Random(); |
86 |
volatile int tokenLoc; //the token is currently here |
87 |
volatile int numBots; |
88 |
volatile int selectedBot; //the user has selected this bot |
89 |
volatile Rectangle[] botRect; //contains boundary shapes around bots for click detection |
90 |
volatile int[] xbeeID; |
91 |
|
92 |
Thread drawThread;
|
93 |
Simulator simulator; |
94 |
SelectionIndicator indicator; |
95 |
PacketMonitor packetMonitor; |
96 |
ColonetServerInterface csi; |
97 |
|
98 |
|
99 |
public void init () { |
100 |
// set the default look and feel - choose one
|
101 |
//String laf = UIManager.getSystemLookAndFeelClassName();
|
102 |
String laf = UIManager.getCrossPlatformLookAndFeelClassName(); |
103 |
//String laf = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
|
104 |
try {
|
105 |
UIManager.setLookAndFeel(laf);
|
106 |
} catch (UnsupportedLookAndFeelException exc) { |
107 |
System.err.println ("Warning: UnsupportedLookAndFeel: " + laf); |
108 |
} catch (Exception exc) { |
109 |
System.err.println ("Error loading " + laf + ": " + exc); |
110 |
} |
111 |
// We should invoke and wait to avoid browser display difficulties
|
112 |
Runnable r = new Runnable() { |
113 |
public void run() { |
114 |
createAndShowGUI(); |
115 |
} |
116 |
}; |
117 |
try {
|
118 |
SwingUtilities.invokeAndWait(r);
|
119 |
} catch (InterruptedException e) { |
120 |
//Not really sure why we would be in this situation
|
121 |
System.out.println(e);
|
122 |
} catch (java.lang.reflect.InvocationTargetException e) {
|
123 |
//This could happen for various reasons if there is a problem in createAndShowGUI
|
124 |
e.printStackTrace(); |
125 |
} |
126 |
} |
127 |
|
128 |
public void destroy () { |
129 |
try { drawThread.interrupt(); } catch (Exception e) { } |
130 |
try { indicator.interrupt(); } catch (Exception e) { } |
131 |
try { packetMonitor.interrupt(); } catch (Exception e) { } |
132 |
} |
133 |
|
134 |
private synchronized void createAndShowGUI () { |
135 |
// init graphical elements
|
136 |
// Get the graphics configuration of the screen to create a buffer
|
137 |
gc = GraphicsEnvironment.getLocalGraphicsEnvironment()
|
138 |
.getDefaultScreenDevice().getDefaultConfiguration(); |
139 |
image = gc.createCompatibleImage(CANVAS_SIZE,CANVAS_SIZE); |
140 |
canvas = image.createGraphics(); |
141 |
canvas.setStroke(new BasicStroke(2)); //set pen width |
142 |
panel = new ImagePanel(false, image); //set automatic double-buffering to false. we are doing it manually. |
143 |
|
144 |
// Calculate center of canvas
|
145 |
cx = image.getWidth() / 2;
|
146 |
cy = image.getHeight() / 2;
|
147 |
|
148 |
botFont = new Font("Arial", Font.PLAIN, 14); |
149 |
tokenLoc = 0;
|
150 |
numBots = 0;
|
151 |
selectedBot = 0;
|
152 |
|
153 |
// Connection area
|
154 |
txtMatrix = new JTextArea(); |
155 |
txtMatrix.setBorder(BorderFactory.createTitledBorder("Input Matrix")); |
156 |
txtInfo = new JTextArea(); |
157 |
txtInfo.setBorder(BorderFactory.createTitledBorder("Info")); |
158 |
txtInfo.setEditable(false);
|
159 |
btnGraph = new JButton("Run"); |
160 |
txtHost = new JTextField("roboclub9.frc.ri.cmu.edu"); |
161 |
txtHost.setBorder(BorderFactory.createTitledBorder("Host")); |
162 |
txtPort = new JTextField("10123"); |
163 |
txtPort.setBorder(BorderFactory.createTitledBorder("Port")); |
164 |
btnConnect = new JButton("Connect"); |
165 |
lblConnectionStatus = new JLabel("Status: Offline"); |
166 |
panelConnect = new JPanel(); |
167 |
panelConnect.setLayout(new GridLayout(6,1)); |
168 |
panelConnect.add(lblConnectionStatus); |
169 |
panelConnect.add(txtHost); |
170 |
panelConnect.add(txtPort); |
171 |
panelConnect.add(btnConnect); |
172 |
panelConnect.add(txtInfo); |
173 |
panelConnect.add(btnGraph); |
174 |
panelServerInterface = new JPanel(); |
175 |
panelServerInterface.setLayout(new GridLayout(2,1)); |
176 |
panelServerInterface.add(panelConnect); |
177 |
panelServerInterface.add(txtMatrix); |
178 |
|
179 |
// Status Elements
|
180 |
lblTokenPasses = new JLabel(); |
181 |
lblBattery = new JLabel("???"); |
182 |
panelStats = new JPanel(); |
183 |
panelStats.setLayout(new GridLayout(4,2)); |
184 |
panelStats.add(new JLabel("Token Passes / sec ")); |
185 |
panelStats.add(lblTokenPasses); |
186 |
panelStats.add(new JLabel("Battery ")); |
187 |
panelStats.add(lblBattery); |
188 |
panelStats.add(new JLabel("Token Passes / sec ")); |
189 |
panelStats.add(lblTokenPasses); |
190 |
|
191 |
//TODO: add panelStats somewhere?
|
192 |
|
193 |
// Robot direction panel
|
194 |
panelRobotDirection = new JPanel(); |
195 |
panelRobotDirectionButtons = new JPanel(); |
196 |
btnF = new JButton("^"); |
197 |
btnB = new JButton("v"); |
198 |
btnL = new JButton("<"); |
199 |
btnR = new JButton(">"); |
200 |
btnActivate = new JButton("o"); |
201 |
panelRobotDirectionButtons.setLayout(new GridLayout(1,5)); |
202 |
panelRobotDirectionButtons.add(btnActivate); |
203 |
panelRobotDirectionButtons.add(btnF); |
204 |
panelRobotDirectionButtons.add(btnB); |
205 |
panelRobotDirectionButtons.add(btnL); |
206 |
panelRobotDirectionButtons.add(btnR); |
207 |
|
208 |
imageVectorControl = gc.createCompatibleImage(395, 220); |
209 |
vectorController = new VectorController(imageVectorControl);
|
210 |
panelRobotDirection.setLayout(new BorderLayout()); |
211 |
panelRobotDirection.add(vectorController, BorderLayout.CENTER);
|
212 |
panelRobotDirection.add(panelRobotDirectionButtons, BorderLayout.SOUTH);
|
213 |
|
214 |
// Robot Control and Commands
|
215 |
panelRobotCommands = new JPanel(); |
216 |
panelRobotCommands.setLayout(new GridLayout(5,2)); |
217 |
cmbRobotNum = new JComboBox(); |
218 |
btnCommand_StopTask = new JButton("Stop Current Task"); |
219 |
btnCommand_ResumeTask = new JButton("Resume Current Task"); |
220 |
btnCommand_ChargeNow = new JButton("Recharge Now"); |
221 |
btnCommand_StopCharging = new JButton("Stop Recharging"); |
222 |
panelRobotCommands.add(new JLabel("Select Robot to Control: ")); |
223 |
panelRobotCommands.add(cmbRobotNum); |
224 |
panelRobotCommands.add(btnCommand_StopTask); |
225 |
panelRobotCommands.add(btnCommand_ResumeTask); |
226 |
panelRobotCommands.add(btnCommand_ChargeNow); |
227 |
panelRobotCommands.add(btnCommand_StopCharging); |
228 |
panelRobotControl = new JPanel(); |
229 |
panelRobotControl.setLayout(new GridLayout(2,1)); |
230 |
panelRobotControl.add(panelRobotDirection); |
231 |
panelRobotControl.add(panelRobotCommands); |
232 |
|
233 |
// Task Manager
|
234 |
panelTaskManager = new JPanel(); |
235 |
panelTaskManager.setLayout(new BorderLayout()); |
236 |
taskListModel = new DefaultListModel(); |
237 |
taskList = new JList(taskListModel); |
238 |
taskList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
239 |
taskList.setSelectedIndex(0);
|
240 |
spTaskManager = new JScrollPane(taskList); |
241 |
panelTaskManagerControls = new JPanel(); |
242 |
panelTaskManagerControls.setLayout(new GridLayout(1,4)); |
243 |
panelTaskManagerControlsPriority = new JPanel(); |
244 |
panelTaskManagerControlsPriority.setLayout(new GridLayout(1,2)); |
245 |
btnAddTask = new JButton("Add..."); |
246 |
btnRemoveTask = new JButton("Remove"); |
247 |
btnMoveTaskUp = new JButton("^"); |
248 |
btnMoveTaskDown = new JButton("v"); |
249 |
btnUpdateTasks = new JButton("Update"); |
250 |
panelTaskManagerControlsPriority.add(btnMoveTaskUp); |
251 |
panelTaskManagerControlsPriority.add(btnMoveTaskDown); |
252 |
panelTaskManagerControls.add(btnAddTask); |
253 |
panelTaskManagerControls.add(btnRemoveTask); |
254 |
panelTaskManagerControls.add(btnUpdateTasks); |
255 |
panelTaskManagerControls.add(panelTaskManagerControlsPriority); |
256 |
panelTaskManager.add(spTaskManager, BorderLayout.CENTER);
|
257 |
panelTaskManager.add(panelTaskManagerControls, BorderLayout.SOUTH);
|
258 |
panelTaskManager.add(new JLabel("Current Task Queue"), BorderLayout.NORTH); |
259 |
|
260 |
// Task Manager Add Window
|
261 |
taskAddWindow = new TaskAddWindow();
|
262 |
|
263 |
// Message log
|
264 |
log = new JTextArea(); |
265 |
spLog = new JScrollPane(log, |
266 |
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
|
267 |
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
268 |
spLog.setBorder(BorderFactory.createTitledBorder("Log")); |
269 |
spLog.setPreferredSize(new Dimension(0, 150)); |
270 |
log.setEditable(false);
|
271 |
|
272 |
// Main control mechanism
|
273 |
panelControl = new JPanel(); |
274 |
panelControl.setLayout(new GridLayout(1,1)); |
275 |
tabPaneControl = new JTabbedPane(JTabbedPane.TOP); |
276 |
tabPaneControl.setPreferredSize(new Dimension(400, 0)); |
277 |
tabPaneControl.addTab("Connection", panelServerInterface);
|
278 |
tabPaneControl.addTab("Robots", panelRobotControl);
|
279 |
tabPaneControl.addTab("Tasks", panelTaskManager);
|
280 |
panelControl.add(tabPaneControl); |
281 |
|
282 |
// Set up elements in the south
|
283 |
panelSouth = new JPanel(); |
284 |
panelSouth.setLayout(new GridLayout(1,2)); |
285 |
panelSouth.add(spLog); |
286 |
|
287 |
this.getContentPane().setLayout(new BorderLayout()); |
288 |
this.getContentPane().add(panel, BorderLayout.CENTER); |
289 |
this.getContentPane().add(panelSouth, BorderLayout.SOUTH); |
290 |
this.getContentPane().add(panelControl, BorderLayout.EAST); |
291 |
this.setVisible(true); |
292 |
|
293 |
/* Add all listeners here */
|
294 |
// Task Management
|
295 |
btnAddTask.addActionListener(this);
|
296 |
btnRemoveTask.addActionListener(this);
|
297 |
btnMoveTaskUp.addActionListener(this);
|
298 |
btnMoveTaskDown.addActionListener(this);
|
299 |
btnUpdateTasks.addActionListener(this);
|
300 |
// Robot Control
|
301 |
btnF.addActionListener(this);
|
302 |
btnB.addActionListener(this);
|
303 |
btnL.addActionListener(this);
|
304 |
btnR.addActionListener(this);
|
305 |
btnF.addKeyListener(this);
|
306 |
btnB.addKeyListener(this);
|
307 |
btnL.addKeyListener(this);
|
308 |
btnR.addKeyListener(this);
|
309 |
btnActivate.addActionListener(this);
|
310 |
btnActivate.addKeyListener(this);
|
311 |
vectorController.addMouseMotionListener(this);
|
312 |
vectorController.addMouseListener(this);
|
313 |
btnCommand_StopTask.addActionListener(this);
|
314 |
btnCommand_ResumeTask.addActionListener(this);
|
315 |
btnCommand_ChargeNow.addActionListener(this);
|
316 |
btnCommand_StopCharging.addActionListener(this);
|
317 |
// Other
|
318 |
btnGraph.addActionListener(this);
|
319 |
btnConnect.addActionListener(this);
|
320 |
panel.addMouseListener(this);
|
321 |
this.addMouseMotionListener(this); |
322 |
|
323 |
|
324 |
// Set up dependent threads
|
325 |
indicator = new SelectionIndicator(canvas);
|
326 |
indicator.setRadius(RADIUS+3, 15); //a tad more than the bot radius |
327 |
packetMonitor = new PacketMonitor();
|
328 |
simulator = new Simulator();
|
329 |
|
330 |
csi = new ColonetServerInterface(this); |
331 |
|
332 |
} |
333 |
|
334 |
public synchronized void paint (Graphics g) { |
335 |
/* Redraw the graphical components in the applet.
|
336 |
This paint method overrides the built-in paint of the
|
337 |
JApplet, and we don't want to deal with redrawing the
|
338 |
components manually. Fuck that shit. */
|
339 |
step(); |
340 |
super.paint(g);
|
341 |
} |
342 |
|
343 |
public synchronized void update (Graphics g) { |
344 |
paint(g); |
345 |
} |
346 |
|
347 |
private void randomize () { |
348 |
Random r = new Random(); |
349 |
StringBuilder s = new StringBuilder(); |
350 |
|
351 |
int count = r.nextInt(8) + 1; |
352 |
for (int i = 0; i < count; i++) { |
353 |
for (int j = 0; j < count; j++) { |
354 |
if (r.nextBoolean())
|
355 |
s.append("" + (r.nextInt(16) + 1)); |
356 |
else
|
357 |
s.append("-");
|
358 |
if (j != count-1) |
359 |
s.append(" ");
|
360 |
} |
361 |
if (i != count-1) s.append("\n"); |
362 |
} |
363 |
|
364 |
txtMatrix.setText(s.toString()); |
365 |
} |
366 |
|
367 |
public void drawRobot (int id, int x, int y) { |
368 |
//save the bot in memory, so we can tell if we click on it later
|
369 |
botRect[id] = new Rectangle(x-RADIUS, y-RADIUS, 2*RADIUS, 2*RADIUS); |
370 |
|
371 |
//draw the bot on the canvas
|
372 |
canvas.setColor(Color.BLACK);
|
373 |
canvas.drawOval(x-RADIUS, y-RADIUS, RADIUS*2, RADIUS*2); |
374 |
|
375 |
//draw the label
|
376 |
canvas.setFont(botFont); |
377 |
try {
|
378 |
canvas.drawString("" + xbeeID[id], x-20, y+2); |
379 |
} catch (Exception e) { |
380 |
canvas.drawString("???", x-22, y+2); |
381 |
} |
382 |
} |
383 |
|
384 |
public void drawConnection (int start, int end, int radius, Color color) { |
385 |
final int ARROW_LENGTH = 18; |
386 |
|
387 |
double angle = 2.0 * Math.PI / numBots; |
388 |
int startx, starty, endx, endy;
|
389 |
startx = cx - (int)(radius * Math.cos(start * angle)); |
390 |
starty = cy - (int)(radius * Math.sin(start * angle)); |
391 |
endx = cx - (int)(radius * Math.cos(end * angle)); |
392 |
endy = cy - (int)(radius * Math.sin(end * angle)); |
393 |
canvas.setColor(color); |
394 |
canvas.drawLine(startx, starty, endx, endy); |
395 |
|
396 |
//create arrow
|
397 |
if (color.equals(Color.BLACK)) return; |
398 |
int big_dy = starty - endy;
|
399 |
int big_dx = endx - startx;
|
400 |
double theta = 0; |
401 |
if (big_dx == 0 && starty > endy) //pointing up |
402 |
theta = Math.PI/2; |
403 |
else if (big_dx == 0 && starty < endy) //pointing down |
404 |
theta = 3*Math.PI/2; |
405 |
else if (big_dy == 0 && startx > endx) //pointing left |
406 |
theta = Math.PI;
|
407 |
else if (big_dy == 0 && startx < endx) //pointing right |
408 |
theta = 0;
|
409 |
else
|
410 |
theta = Math.atan(1.0 * big_dy / big_dx); |
411 |
|
412 |
//create ploygon
|
413 |
Polygon poly = new Polygon(); |
414 |
int dx_arrow = Math.abs((int)(ARROW_LENGTH * Math.cos(theta))); |
415 |
int dy_arrow = Math.abs((int)(ARROW_LENGTH * Math.sin(theta))); |
416 |
int dy_half = (int)(ARROW_LENGTH/2 * Math.cos(theta)); |
417 |
int dx_half = (int)(ARROW_LENGTH/2 * Math.sin(theta)); |
418 |
int rx = (big_dx > 0) ? endx - dx_arrow : endx + dx_arrow; |
419 |
int ry = (big_dy > 0) ? endy + dy_arrow : endy - dy_arrow; |
420 |
poly.addPoint(endx, endy); |
421 |
poly.addPoint(rx - dx_half, ry - dy_half); |
422 |
poly.addPoint(rx + dx_half, ry + dy_half); |
423 |
canvas.fillPolygon(poly); |
424 |
} |
425 |
|
426 |
public void run () { |
427 |
while (true) { |
428 |
repaint(); |
429 |
try {
|
430 |
Thread.sleep(90); |
431 |
} catch (InterruptedException e) { |
432 |
return;
|
433 |
} |
434 |
} |
435 |
} |
436 |
|
437 |
public void step () { |
438 |
final int DIAMETER = image.getWidth() - 2*BUFFER; |
439 |
final int BIGRADIUS = DIAMETER / 2; |
440 |
final int TOKENRADIUS = 40; |
441 |
boolean valid;
|
442 |
|
443 |
// clear image
|
444 |
canvas.setColor(Color.WHITE);
|
445 |
canvas.fillRect(0, 0, image.getWidth(), image.getHeight()); |
446 |
|
447 |
// parse the matrix, to see what robots exist
|
448 |
String [] rows = txtMatrix.getText().split("\n"); |
449 |
numBots = rows.length; |
450 |
String [][] entries = new String[numBots][numBots]; |
451 |
valid = true;
|
452 |
for (int i = 0; i < numBots; i++) { |
453 |
entries[i] = rows[i].split(" ");
|
454 |
if (entries[i].length != rows.length) valid = false; |
455 |
} |
456 |
|
457 |
if (valid) {
|
458 |
this.showStatus("Running"); |
459 |
|
460 |
// draw robots and find which one is seleced
|
461 |
double angle = 2.0 * Math.PI / numBots; |
462 |
canvas.setColor(Color.BLACK);
|
463 |
botRect = new Rectangle[numBots]; |
464 |
int x, y;
|
465 |
if (selectedBot >= numBots) selectedBot = 0; |
466 |
for (int i = 0; i < numBots; i++) { |
467 |
x = cx - (int)(BIGRADIUS * Math.cos(i * angle)); |
468 |
y = cy - (int)(BIGRADIUS * Math.sin(i * angle)); |
469 |
drawRobot(i, x, y); |
470 |
if (i == selectedBot) indicator.setCenter(x, y);
|
471 |
} |
472 |
|
473 |
// draw token marker
|
474 |
int tokenx, tokeny;
|
475 |
int tokenNum = tokenLoc;
|
476 |
tokenx = cx - (int)(BIGRADIUS * Math.cos(tokenNum * angle)); |
477 |
tokeny = cy - (int)(BIGRADIUS * Math.sin(tokenNum * angle)); |
478 |
canvas.setColor(Color.RED);
|
479 |
canvas.drawOval(tokenx-TOKENRADIUS, tokeny-TOKENRADIUS, 2*TOKENRADIUS, 2*TOKENRADIUS); |
480 |
|
481 |
// create an inner circle along which the connections are made.
|
482 |
// let the diameter of this circle be 2*RADIUS less than the outerDiameter.
|
483 |
// see what connections exist
|
484 |
for (int row = 0; row < numBots; row++) { |
485 |
for(int col = 0; col < numBots; col++) { |
486 |
if (!entries[row][col].equals("-") && entries[col][row].equals("-") && row != col) { |
487 |
//TODO: Make a standard gray
|
488 |
drawConnection(row, col, BIGRADIUS-RADIUS, new Color(200,200,200)); |
489 |
} else if (!entries[row][col].equals("-") && ! entries[col][row].equals("-") && row != col) { |
490 |
drawConnection(row, col, BIGRADIUS-RADIUS, Color.BLACK);
|
491 |
} |
492 |
} |
493 |
} |
494 |
|
495 |
// draw the selection indicator
|
496 |
indicator.draw(); |
497 |
|
498 |
} else {// if matrix is not valid |
499 |
this.showStatus("Error: Invalid matrix"); |
500 |
} |
501 |
|
502 |
} |
503 |
|
504 |
/* At this point, moveToken is only called by the simulator.
|
505 |
* In the future, it can be rewritten to account for non-standard
|
506 |
* token passing or deleted if the information can be retrieved
|
507 |
* directly from the Colonet server instead.
|
508 |
*/
|
509 |
public void moveToken () { |
510 |
try {
|
511 |
tokenLoc = (tokenLoc+1)%numBots;
|
512 |
} catch (ArithmeticException e) { // in case numRobots is zero |
513 |
} |
514 |
|
515 |
packetMonitor.addTokenPass(); |
516 |
} |
517 |
|
518 |
public JTextArea getLog () { |
519 |
return log;
|
520 |
} |
521 |
|
522 |
public JTextArea getMatrixInput () { |
523 |
return txtMatrix;
|
524 |
} |
525 |
|
526 |
public void parseMatrix (String line) { |
527 |
txtMatrix.setText("");
|
528 |
String [] str = line.split(" "); |
529 |
int num = Integer.parseInt(str[2]); |
530 |
for (int i = 0; i < num; i++) { |
531 |
for (int j = 0; j < num; j++) { |
532 |
String next = str[3 + i*num + j]; |
533 |
if (next.equals("-1")) |
534 |
txtMatrix.append("-");
|
535 |
else
|
536 |
txtMatrix.append(next); |
537 |
if (j < num - 1) |
538 |
txtMatrix.append(" ");
|
539 |
} |
540 |
if (i < num - 1) |
541 |
txtMatrix.append("\n");
|
542 |
} |
543 |
|
544 |
} |
545 |
|
546 |
public void parseQueue (String line) { |
547 |
log.append("Got queue update\n");
|
548 |
|
549 |
} |
550 |
|
551 |
public void parseXBeeIDs (String line) { |
552 |
String [] str = line.split(" "); |
553 |
int num = Integer.parseInt(str[2]); |
554 |
xbeeID = new int[num]; |
555 |
for (int i = 0; i < num; i++) |
556 |
xbeeID[i] = Integer.parseInt(str[i+3]); |
557 |
|
558 |
//update the list of robots to control
|
559 |
cmbRobotNum.removeAllItems(); |
560 |
cmbRobotNum.addItem(new String(" All ")); |
561 |
for (int i = 0; i < num; i++) |
562 |
cmbRobotNum.addItem(new String("" + xbeeID[i])); |
563 |
} |
564 |
|
565 |
//
|
566 |
// MouseListener methods
|
567 |
//
|
568 |
public void mouseExited(MouseEvent e) { |
569 |
} |
570 |
public void mouseEntered(MouseEvent e) { |
571 |
} |
572 |
public void mouseReleased(MouseEvent e) { |
573 |
vectorController.notifyMouseEvent(e, true);
|
574 |
} |
575 |
public void mouseClicked(MouseEvent e) { |
576 |
vectorController.notifyMouseEvent(e, false);
|
577 |
} |
578 |
public void mousePressed(MouseEvent e) { |
579 |
try {
|
580 |
for (int i = 0; i < numBots; i++) { |
581 |
if (botRect[i].contains(e.getPoint()))
|
582 |
selectedBot = i; |
583 |
} |
584 |
} catch (Exception ex) { |
585 |
System.out.println(e);
|
586 |
} |
587 |
|
588 |
} |
589 |
public void mouseDragged(MouseEvent e) { |
590 |
vectorController.notifyMouseEvent(e, false);
|
591 |
} |
592 |
public void mouseMoved(MouseEvent e) { |
593 |
} |
594 |
|
595 |
//
|
596 |
// KeyListener methods
|
597 |
//
|
598 |
public void keyPressed (KeyEvent e) { |
599 |
int code = e.getKeyCode();
|
600 |
if (code == KeyEvent.VK_UP) { |
601 |
vectorController.setMaxForward(); |
602 |
vectorController.sendToServer(); |
603 |
} else if (code == KeyEvent.VK_DOWN) { |
604 |
vectorController.setMaxReverse(); |
605 |
vectorController.sendToServer(); |
606 |
} else if (code == KeyEvent.VK_LEFT) { |
607 |
vectorController.setMaxLeft(); |
608 |
vectorController.sendToServer(); |
609 |
} else if (code == KeyEvent.VK_RIGHT) { |
610 |
vectorController.setMaxRight(); |
611 |
vectorController.sendToServer(); |
612 |
} else if (code == KeyEvent.VK_S) { |
613 |
vectorController.setZero(); |
614 |
vectorController.sendToServer(); |
615 |
} |
616 |
} |
617 |
public void keyReleased (KeyEvent e) { |
618 |
} |
619 |
public void keyTyped (KeyEvent e) { |
620 |
} |
621 |
|
622 |
|
623 |
//
|
624 |
// ActionListener method
|
625 |
//
|
626 |
public void actionPerformed (ActionEvent e) { |
627 |
Object source = e.getSource();
|
628 |
if (source == btnGraph) {
|
629 |
btnGraph.setEnabled(false);
|
630 |
//Start dependent threads
|
631 |
drawThread = new Thread(this, "drawThread"); |
632 |
drawThread.start(); |
633 |
indicator.start(); |
634 |
packetMonitor.start(); |
635 |
simulator.start(); |
636 |
} else if (source == btnConnect) { |
637 |
csi.connect(txtHost.getText(), txtPort.getText()); |
638 |
dataUpdater = new DataUpdater();
|
639 |
dataUpdater.start(); |
640 |
} |
641 |
// Robot Movement Controls
|
642 |
else if (source == btnF) { |
643 |
vectorController.setMaxForward(); |
644 |
vectorController.sendToServer(); |
645 |
} else if (source == btnB) { |
646 |
vectorController.setMaxReverse(); |
647 |
vectorController.sendToServer(); |
648 |
} else if (source == btnL) { |
649 |
vectorController.setMaxLeft(); |
650 |
vectorController.sendToServer(); |
651 |
} else if (source == btnR) { |
652 |
vectorController.setMaxRight(); |
653 |
vectorController.sendToServer(); |
654 |
} else if (source == btnActivate) { |
655 |
vectorController.setZero(); |
656 |
vectorController.sendToServer(); |
657 |
} |
658 |
// Robot Commands (non-movement)
|
659 |
else if (source == btnCommand_StopTask) { |
660 |
|
661 |
} else if (source == btnCommand_ResumeTask) { |
662 |
|
663 |
} else if (source == btnCommand_ChargeNow) { |
664 |
|
665 |
} else if (source == btnCommand_StopCharging) { |
666 |
|
667 |
} |
668 |
|
669 |
// Queue Management
|
670 |
else if (source == btnAddTask) { |
671 |
taskAddWindow.prompt(); |
672 |
} else if (source == btnRemoveTask) { |
673 |
if (taskList.getSelectedIndex() >= 0); |
674 |
csi.sendQueueRemove(taskList.getSelectedIndex()); |
675 |
csi.sendQueueUpdate(); |
676 |
} else if (source == btnMoveTaskUp) { |
677 |
csi.sendQueueReorder(taskList.getSelectedIndex(), taskList.getSelectedIndex() - 1);
|
678 |
csi.sendQueueUpdate(); |
679 |
} else if (source == btnMoveTaskDown) { |
680 |
csi.sendQueueReorder(taskList.getSelectedIndex(), taskList.getSelectedIndex() + 1);
|
681 |
csi.sendQueueUpdate(); |
682 |
} else if (source == btnUpdateTasks) { |
683 |
csi.sendQueueUpdate(); |
684 |
} |
685 |
} |
686 |
|
687 |
/*
|
688 |
* SelectionIndicator thread.
|
689 |
* Graphical representation of the selection marker
|
690 |
*
|
691 |
* step() and draw() are synchronized methods. step() is private and
|
692 |
* used to update the position of the crosshairs. draw() is called
|
693 |
* externally and should only run if all calculations in step() have
|
694 |
* been completed.
|
695 |
*/
|
696 |
private class SelectionIndicator extends Thread { |
697 |
|
698 |
final int INDICATOR_DELAY = 100; |
699 |
final double DTHETA = 0.4; //larger values make the marker rotate faster |
700 |
Graphics2D g; //canvas to draw on |
701 |
boolean running;
|
702 |
|
703 |
int sx, sy; //center |
704 |
int r, dr; //radius and width of marker |
705 |
double theta; //current angle |
706 |
|
707 |
volatile Polygon poly1, poly2, poly3, poly4; |
708 |
|
709 |
int px1, py1;
|
710 |
int rx1, ry1;
|
711 |
int px2, py2;
|
712 |
int rx2, ry2;
|
713 |
int px3, py3;
|
714 |
int rx3, ry3;
|
715 |
int px4, py4;
|
716 |
int rx4, ry4;
|
717 |
|
718 |
int steps;
|
719 |
|
720 |
public SelectionIndicator (Graphics2D g) { |
721 |
super("SelectionIndicator"); |
722 |
this.g = g;
|
723 |
running = false;
|
724 |
steps = 0;
|
725 |
|
726 |
theta = 0;
|
727 |
rx1 = 0; ry1 = 0; |
728 |
px1 = 0; py1 = 0; |
729 |
rx2 = 0; ry2 = 0; |
730 |
px2 = 0; py2 = 0; |
731 |
rx3 = 0; ry3 = 0; |
732 |
px3 = 0; py3 = 0; |
733 |
rx4 = 0; ry4 = 0; |
734 |
px4 = 0; py4 = 0; |
735 |
} |
736 |
|
737 |
public synchronized void setCenter (int sx, int sy) { |
738 |
if (sx == this.sx && sy == this.sy) return; |
739 |
this.sx = sx;
|
740 |
this.sy = sy;
|
741 |
steps = 0;
|
742 |
} |
743 |
|
744 |
public synchronized void setRadius (int r, int dr) { |
745 |
this.r = r;
|
746 |
this.dr = dr;
|
747 |
steps = 0;
|
748 |
} |
749 |
|
750 |
public void run () { |
751 |
running = true;
|
752 |
while (running) {
|
753 |
step(); |
754 |
try {
|
755 |
Thread.sleep(INDICATOR_DELAY);
|
756 |
} catch (InterruptedException e) { |
757 |
running = false;
|
758 |
return;
|
759 |
} |
760 |
} |
761 |
} |
762 |
|
763 |
private synchronized void step () { |
764 |
Polygon poly1_new = new Polygon(); |
765 |
Polygon poly2_new = new Polygon(); |
766 |
Polygon poly3_new = new Polygon(); |
767 |
Polygon poly4_new = new Polygon(); |
768 |
|
769 |
//the step
|
770 |
theta = (theta + DTHETA/Math.PI) % (Math.PI); |
771 |
|
772 |
//the calculation
|
773 |
//let p be the point of the pointy thing toward the center
|
774 |
//let r be the point at the opposite side
|
775 |
|
776 |
//recalculate radius, if it will look cool, lolz
|
777 |
int newr = r;
|
778 |
if (steps < 100) |
779 |
newr = (int)( r + 200/(steps+1) ); |
780 |
|
781 |
//precompute values for dx and dy
|
782 |
int dx_inner = (int)(newr * Math.cos(theta)); |
783 |
int dy_inner = (int)(newr * Math.sin(theta)); |
784 |
int dx_outer = (int)((newr+dr) * Math.cos(theta)); |
785 |
int dy_outer = (int)((newr+dr) * Math.sin(theta)); |
786 |
|
787 |
//calculate polygon constants
|
788 |
int dy_poly = (int)(dr/2 * Math.cos(theta)); |
789 |
int dx_poly = (int)(dr/2 * Math.sin(theta)); |
790 |
|
791 |
//determine critical points
|
792 |
//kansas city shuffle!
|
793 |
px1 = sx + dx_inner; |
794 |
py1 = sy - dy_inner; |
795 |
rx1 = sx + dx_outer; |
796 |
ry1 = sy - dy_outer; |
797 |
px2 = sx - dx_inner; |
798 |
py2 = sy + dy_inner; |
799 |
rx2 = sx - dx_outer; |
800 |
ry2 = sy + dy_outer; |
801 |
px3 = sx - dy_inner; |
802 |
py3 = sy - dx_inner; |
803 |
rx3 = sx - dy_outer; |
804 |
ry3 = sy - dx_outer; |
805 |
px4 = sx + dy_inner; |
806 |
py4 = sy + dx_inner; |
807 |
rx4 = sx + dy_outer; |
808 |
ry4 = sy + dx_outer; |
809 |
|
810 |
//create polygons
|
811 |
poly1_new.addPoint(px1, py1); |
812 |
poly1_new.addPoint(rx1+dx_poly, ry1+dy_poly); |
813 |
poly1_new.addPoint(rx1-dx_poly, ry1-dy_poly); |
814 |
poly2_new.addPoint(px2, py2); |
815 |
poly2_new.addPoint(rx2+dx_poly, ry2+dy_poly); |
816 |
poly2_new.addPoint(rx2-dx_poly, ry2-dy_poly); |
817 |
poly3_new.addPoint(px3, py3); |
818 |
poly3_new.addPoint(rx3-dy_poly, ry3+dx_poly); |
819 |
poly3_new.addPoint(rx3+dy_poly, ry3-dx_poly); |
820 |
poly4_new.addPoint(px4, py4); |
821 |
poly4_new.addPoint(rx4-dy_poly, ry4+dx_poly); |
822 |
poly4_new.addPoint(rx4+dy_poly, ry4-dx_poly); |
823 |
|
824 |
//reassign updated polygons
|
825 |
poly1 = poly1_new; |
826 |
poly2 = poly2_new; |
827 |
poly3 = poly3_new; |
828 |
poly4 = poly4_new; |
829 |
|
830 |
if (steps < 300) steps++; |
831 |
} |
832 |
|
833 |
public synchronized void draw () { |
834 |
if (!running) return; |
835 |
g.setColor(Color.GRAY);
|
836 |
//draw polygons
|
837 |
g.fillPolygon(poly1); |
838 |
g.fillPolygon(poly2); |
839 |
g.fillPolygon(poly3); |
840 |
g.fillPolygon(poly4); |
841 |
} |
842 |
|
843 |
} |
844 |
|
845 |
/*
|
846 |
* Simulator thread.
|
847 |
*
|
848 |
*/
|
849 |
private class Simulator extends Thread { |
850 |
final int SIMULATOR_DELAY = 300; |
851 |
boolean running;
|
852 |
|
853 |
public Simulator () {
|
854 |
super("Simulator"); |
855 |
running = false;
|
856 |
} |
857 |
|
858 |
public void run () { |
859 |
running = true;
|
860 |
while (running) {
|
861 |
step(); |
862 |
try {
|
863 |
Thread.sleep(SIMULATOR_DELAY);
|
864 |
} catch (InterruptedException e) { |
865 |
running = false;
|
866 |
return;
|
867 |
} |
868 |
} |
869 |
} |
870 |
|
871 |
private void step () { |
872 |
// don't do anything! the colonet should work on its own!
|
873 |
} |
874 |
|
875 |
} |
876 |
|
877 |
/*
|
878 |
* PacketMonitor thread.
|
879 |
*
|
880 |
* Currently, this counts the rate of token passes but will eventually
|
881 |
* be modified to keep more important statistics.
|
882 |
*/
|
883 |
private class PacketMonitor extends Thread { |
884 |
final int PACKETMONITOR_DELAY = 1000; |
885 |
|
886 |
boolean running;
|
887 |
int tokenPasses;
|
888 |
|
889 |
public PacketMonitor () {
|
890 |
super("PacketMonitor"); |
891 |
running = false;
|
892 |
tokenPasses = 0;
|
893 |
} |
894 |
|
895 |
public void run () { |
896 |
running = true;
|
897 |
while (running) {
|
898 |
displayTokenPasses(); |
899 |
try {
|
900 |
Thread.sleep(PACKETMONITOR_DELAY);
|
901 |
} catch (InterruptedException e) { |
902 |
running = false;
|
903 |
return;
|
904 |
} |
905 |
} |
906 |
} |
907 |
|
908 |
public synchronized void addTokenPass () { |
909 |
tokenPasses++; |
910 |
} |
911 |
|
912 |
public synchronized void displayTokenPasses () { |
913 |
lblTokenPasses.setText("" + tokenPasses);
|
914 |
tokenPasses = 0;
|
915 |
} |
916 |
|
917 |
} |
918 |
|
919 |
/*
|
920 |
* DataUpdater thread.
|
921 |
* The purpose of this thread is to request data from the server at regular intervals.
|
922 |
*
|
923 |
*/
|
924 |
class DataUpdater extends Thread { |
925 |
final int DATAUPDATER_DELAY = 5000; |
926 |
|
927 |
public DataUpdater () {
|
928 |
super("Colonet DataUpdater"); |
929 |
} |
930 |
|
931 |
public void run () { |
932 |
String line;
|
933 |
while (true) { |
934 |
try {
|
935 |
//request more data
|
936 |
if (csi.isReady()) {
|
937 |
csi.sendSensorDataRequest(); |
938 |
csi.sendXBeeIDRequest(); |
939 |
} |
940 |
Thread.sleep(DATAUPDATER_DELAY);
|
941 |
} catch (InterruptedException e) { |
942 |
return;
|
943 |
} |
944 |
} |
945 |
} |
946 |
|
947 |
} |
948 |
|
949 |
/*
|
950 |
* ImagePanel class
|
951 |
* Enables more efficient image handling in a component-controlled environment
|
952 |
*/
|
953 |
class ImagePanel extends JPanel { |
954 |
protected Image img; |
955 |
|
956 |
public ImagePanel (Image img) { |
957 |
super();
|
958 |
this.img = img;
|
959 |
} |
960 |
|
961 |
public ImagePanel (boolean isDoubleBuffered, Image img) { |
962 |
super(isDoubleBuffered);
|
963 |
this.img = img;
|
964 |
} |
965 |
|
966 |
public void paint (Graphics g) { |
967 |
// Place the buffered image on the screen, inside the panel
|
968 |
g.drawImage(img, 0, 0, Color.WHITE, this); |
969 |
} |
970 |
|
971 |
} |
972 |
|
973 |
/*
|
974 |
* VectorController class
|
975 |
* Manages robot motion control graphically
|
976 |
*/
|
977 |
class VectorController extends ImagePanel { |
978 |
int x, y, cx, cy;
|
979 |
int width, height;
|
980 |
int side;
|
981 |
|
982 |
public VectorController (Image img) { |
983 |
super (img);
|
984 |
width = img.getWidth(null);
|
985 |
height = img.getHeight(null);
|
986 |
cx = img.getWidth(null)/2; |
987 |
cy = img.getHeight(null)/2; |
988 |
x = cx; |
989 |
y = cy; |
990 |
if (width < height)
|
991 |
side = width; |
992 |
else
|
993 |
side = height; |
994 |
} |
995 |
|
996 |
public void setPoint (int x, int y) { |
997 |
if (!isValidPoint(x, y))
|
998 |
return;
|
999 |
this.x = x;
|
1000 |
this.y = y;
|
1001 |
repaint(); |
1002 |
} |
1003 |
|
1004 |
public boolean isValidPoint (int x, int y) { |
1005 |
double xterm = Math.pow(1.0*(x - cx)/(side/2), 2); |
1006 |
double yterm = Math.pow(1.0*(y - cy)/(side/2), 2); |
1007 |
return (xterm + yterm <= 1); |
1008 |
} |
1009 |
|
1010 |
public void notifyMouseEvent (MouseEvent e, boolean send) { |
1011 |
if (!isValidPoint(e.getX(), e.getY()))
|
1012 |
return;
|
1013 |
vectorController.setPoint(e.getX(), e.getY()); |
1014 |
vectorController.repaint(); |
1015 |
if (send)
|
1016 |
vectorController.sendToServer(); |
1017 |
} |
1018 |
|
1019 |
public int getVelocity () { |
1020 |
int dx = x - cx;
|
1021 |
int dy = y - cy;
|
1022 |
int v = (int) Math.sqrt( Math.pow(dx, 2) + Math.pow(dy, 2) ); |
1023 |
return v;
|
1024 |
} |
1025 |
|
1026 |
public int getAngle () { |
1027 |
int dx = x - cx;
|
1028 |
int dy = cy - y;
|
1029 |
double theta = Math.atan2(Math.abs(dx), Math.abs(dy)); |
1030 |
theta = (int) (1.0 * theta * 180 / Math.PI); //transform to degrees |
1031 |
if (dy < 0) |
1032 |
theta = 180 - theta;
|
1033 |
theta *= Math.signum(dx);
|
1034 |
return (int) theta; |
1035 |
} |
1036 |
|
1037 |
public void paint (Graphics g) { |
1038 |
g.setColor(Color.BLACK);
|
1039 |
g.fillRect(0, 0, width, height); |
1040 |
((Graphics2D)g).setStroke(new BasicStroke(1)); |
1041 |
g.setColor(Color.RED);
|
1042 |
g.drawOval(cx-side/2, cy-side/2, side, side); |
1043 |
((Graphics2D)g).setStroke(new BasicStroke(2)); |
1044 |
g.setColor(Color.GREEN);
|
1045 |
g.drawLine(cx, cy, x, y); |
1046 |
g.fillOval(x-3, y-3, 6, 6); |
1047 |
} |
1048 |
|
1049 |
public void setMaxForward () { |
1050 |
setPoint(cx, cy - (side/2) + 1); |
1051 |
} |
1052 |
|
1053 |
public void setMaxReverse () { |
1054 |
setPoint(cx, cy + (side/2) - 1); |
1055 |
} |
1056 |
|
1057 |
public void setMaxLeft () { |
1058 |
setPoint(cx - (side/2) + 1, cy); |
1059 |
} |
1060 |
|
1061 |
public void setMaxRight () { |
1062 |
setPoint(cx + (side/2) - 1, cy); |
1063 |
} |
1064 |
|
1065 |
public void setZero () { |
1066 |
setPoint(cx, cy); |
1067 |
} |
1068 |
|
1069 |
public void sendToServer () { |
1070 |
//log.append("Attempitng to send angle = " + vectorController.getAngle() + ", velocity = " + vectorController.getVelocity() + "\n");
|
1071 |
if (csi != null) { |
1072 |
if (cx == 0 && cy == 0) { |
1073 |
csi.sendData(ColonetServerInterface.MOTORS_OFF, ColonetServerInterface.GLOBAL_DEST); |
1074 |
} else {
|
1075 |
//Directional commands
|
1076 |
if (x > cx && y == cy) { //move right |
1077 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " 0 160", ColonetServerInterface.GLOBAL_DEST);
|
1078 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " 1 160", ColonetServerInterface.GLOBAL_DEST);
|
1079 |
} else if (x < cx && y == cy) { //move left |
1080 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " 1 160", ColonetServerInterface.GLOBAL_DEST);
|
1081 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " 0 160", ColonetServerInterface.GLOBAL_DEST);
|
1082 |
} else if (x == cx && y > cy) { //move forward |
1083 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " 0 200", ColonetServerInterface.GLOBAL_DEST);
|
1084 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " 0 200", ColonetServerInterface.GLOBAL_DEST);
|
1085 |
} else if (x == cx && y < cy) { //move backward |
1086 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " 1 200", ColonetServerInterface.GLOBAL_DEST);
|
1087 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " 1 200", ColonetServerInterface.GLOBAL_DEST);
|
1088 |
} else if (x == cx && y == cy) { //stop! |
1089 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " 1 0", ColonetServerInterface.GLOBAL_DEST);
|
1090 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " 1 0", ColonetServerInterface.GLOBAL_DEST);
|
1091 |
} |
1092 |
|
1093 |
/*
|
1094 |
//Work on this later.
|
1095 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " " + "0" + " " +
|
1096 |
(vectorController.getVelocity() + vectorController.getAngle()), ColonetServerInterface.GLOBAL_DEST);
|
1097 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " " + "0" + " " +
|
1098 |
(vectorController.getVelocity() - vectorController.getAngle()), ColonetServerInterface.GLOBAL_DEST);
|
1099 |
*/
|
1100 |
} |
1101 |
} |
1102 |
/*
|
1103 |
//The move command doesn't really work.
|
1104 |
csi.sendData(ColonetServerInterface.MOVE + " " +
|
1105 |
vectorController.getAngle() + " " +
|
1106 |
vectorController.getVelocity(), ColonetServerInterface.GLOBAL_DEST);
|
1107 |
*/
|
1108 |
|
1109 |
} |
1110 |
|
1111 |
} |
1112 |
|
1113 |
/*
|
1114 |
* TaskAddWindow class
|
1115 |
* makes it easy to add tasks to the queue
|
1116 |
*/
|
1117 |
class TaskAddWindow extends JFrame implements ActionListener, ListSelectionListener { |
1118 |
JPanel panelButtons;
|
1119 |
JPanel panelParameters;
|
1120 |
JPanel panelSouth;
|
1121 |
JPanel panelSelection;
|
1122 |
JButton btnSubmit;
|
1123 |
JButton btnCancel;
|
1124 |
DefaultListModel availableListModel;
|
1125 |
JList availableList;
|
1126 |
JScrollPane spAvailableTasks;
|
1127 |
JTextArea txtDescription;
|
1128 |
JTextField txtParameters;
|
1129 |
MouseListener mouseListener;
|
1130 |
|
1131 |
public TaskAddWindow () {
|
1132 |
super("Add a Task"); |
1133 |
super.setSize(500,500); |
1134 |
super.setLayout(new BorderLayout()); |
1135 |
|
1136 |
// set up buttons
|
1137 |
btnSubmit = new JButton("Submit"); |
1138 |
btnCancel = new JButton("Cancel"); |
1139 |
panelButtons = new JPanel(); |
1140 |
panelButtons.setLayout(new FlowLayout()); |
1141 |
panelButtons.add(btnSubmit); |
1142 |
panelButtons.add(btnCancel); |
1143 |
this.getRootPane().setDefaultButton(btnSubmit);
|
1144 |
|
1145 |
// set up task list
|
1146 |
availableListModel = new DefaultListModel(); |
1147 |
availableListModel.addElement("Map the Environment");
|
1148 |
availableListModel.addElement("Clean Up Chemical Spill");
|
1149 |
availableListModel.addElement("Grow Plants");
|
1150 |
availableListModel.addElement("Save the Cheerleader");
|
1151 |
availableListModel.addElement("Save the World");
|
1152 |
availableList = new JList(availableListModel); |
1153 |
availableList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
1154 |
availableList.setSelectedIndex(-1);
|
1155 |
spAvailableTasks = new JScrollPane(availableList); |
1156 |
spAvailableTasks.setBorder(BorderFactory.createTitledBorder("Select A Task")); |
1157 |
txtDescription = new JTextArea(); |
1158 |
txtDescription.setEditable(false);
|
1159 |
txtDescription.setLineWrap(true);
|
1160 |
txtDescription.setWrapStyleWord(true);
|
1161 |
txtDescription.setBorder(BorderFactory.createTitledBorder("Description")); |
1162 |
|
1163 |
//set up parameter area
|
1164 |
panelParameters = new JPanel(); |
1165 |
panelParameters.setLayout(new BorderLayout()); |
1166 |
txtParameters = new JTextField(); |
1167 |
panelParameters.add(new JLabel("Optional parameters for this task: "), BorderLayout.WEST); |
1168 |
panelParameters.add(txtParameters); |
1169 |
|
1170 |
// assemble objects
|
1171 |
panelSelection = new JPanel(); |
1172 |
panelSelection.setLayout(new GridLayout(1,2)); |
1173 |
panelSelection.add(spAvailableTasks); |
1174 |
panelSelection.add(txtDescription); |
1175 |
|
1176 |
panelSouth = new JPanel(); |
1177 |
panelSouth.setLayout(new GridLayout(2,1)); |
1178 |
panelSouth.add(panelParameters); |
1179 |
panelSouth.add(panelButtons); |
1180 |
|
1181 |
this.getContentPane().add(panelSouth, BorderLayout.SOUTH); |
1182 |
this.getContentPane().add(panelSelection, BorderLayout.CENTER); |
1183 |
this.setLocationRelativeTo(null); |
1184 |
|
1185 |
// add listeners here
|
1186 |
availableList.addListSelectionListener(this);
|
1187 |
btnSubmit.addActionListener(this);
|
1188 |
btnCancel.addActionListener(this);
|
1189 |
} |
1190 |
|
1191 |
public void prompt () { |
1192 |
this.setVisible(true); |
1193 |
} |
1194 |
|
1195 |
private String getDescription (int index) { |
1196 |
if (index < 0) |
1197 |
return ""; |
1198 |
switch (index) {
|
1199 |
case 0: return "SLAM and junk"; |
1200 |
case 1: return "I'm not sure this works"; |
1201 |
case 2: return "Push them into the light"; |
1202 |
case 3: return "Watch out for clock repair guys"; |
1203 |
case 4: return "But make sure he's dead, geez, why would you let him get away? I mean come on, " |
1204 |
+"he's just lying there and everyone's too busy looking at people flying through the sky to "
|
1205 |
+"notice? Oh yeah, that's a good transition to a new season, let's make the audience think "
|
1206 |
+"he's dead and then pull a fast one on them, they'll never see that coming.";
|
1207 |
|
1208 |
default: return "Task not recognized"; |
1209 |
} |
1210 |
} |
1211 |
|
1212 |
public void actionPerformed (ActionEvent e) { |
1213 |
Object source = e.getSource();
|
1214 |
if (source == btnSubmit) {
|
1215 |
txtParameters.setText(txtParameters.getText().trim()); |
1216 |
|
1217 |
|
1218 |
this.setVisible(false); |
1219 |
} else if (source == btnCancel) { |
1220 |
this.setVisible(false); |
1221 |
} |
1222 |
} |
1223 |
|
1224 |
public void valueChanged (ListSelectionEvent e) { |
1225 |
int index = availableList.getSelectedIndex();
|
1226 |
if (index >= 0) |
1227 |
txtDescription.setText(getDescription(index)); |
1228 |
} |
1229 |
|
1230 |
} |
1231 |
|
1232 |
} |