root / trunk / code / projects / colonet / ColonetGUI / Colonet.java @ 67
History | View | Annotate | Download (21.9 KB)
1 |
//
|
---|---|
2 |
// Colonet.java
|
3 |
//
|
4 |
|
5 |
import javax.swing.*; |
6 |
import java.awt.*; |
7 |
import java.awt.image.*; |
8 |
import java.awt.event.*; |
9 |
import java.net.*; |
10 |
import java.io.*; |
11 |
import java.util.Random; |
12 |
import java.applet.*; |
13 |
|
14 |
public class Colonet extends JApplet implements ActionListener, MouseListener, Runnable { |
15 |
|
16 |
final int CANVAS_SIZE = 500; //don't make this too large, or the applet will be slow. |
17 |
final int BUFFER = 50; |
18 |
final int RADIUS = 30; |
19 |
|
20 |
// Connection
|
21 |
JTextField txtHost;
|
22 |
JTextField txtPort;
|
23 |
JButton btnConnect;
|
24 |
JButton btnGraph;
|
25 |
JLabel lblConnectionStatus;
|
26 |
JTextArea txtInput;
|
27 |
JTextArea txtInfo;
|
28 |
JPanel panelConnect;
|
29 |
JPanel panelServerInterface;
|
30 |
|
31 |
// Stats
|
32 |
JLabel lblBattery;
|
33 |
JLabel lblTokenPasses;
|
34 |
JLabel lblHastToken;
|
35 |
JPanel panelStats;
|
36 |
|
37 |
// South
|
38 |
JPanel panelSouth;
|
39 |
JTextArea log;
|
40 |
JScrollPane spLog;
|
41 |
|
42 |
// Control
|
43 |
JPanel panelControl;
|
44 |
JTabbedPane tabPaneControl;
|
45 |
JPanel panelRobotControl;
|
46 |
JPanel panelRobotDirection;
|
47 |
JPanel panelRobotCommands;
|
48 |
JButton btnF, btnB, btnL, btnR, btnActivate;
|
49 |
JComboBox cmbRobotNum;
|
50 |
|
51 |
// Task Manager
|
52 |
JPanel panelTaskManager;
|
53 |
JScrollPane spTaskManager;
|
54 |
JPanel panelTaskManagerControls;
|
55 |
JPanel panelTaskManagerControlsPriority;
|
56 |
DefaultListModel taskListModel;
|
57 |
JList taskList;
|
58 |
JButton btnAddTask;
|
59 |
JButton btnRemoveTask;
|
60 |
JButton btnMoveTaskUp;
|
61 |
JButton btnMoveTaskDown;
|
62 |
|
63 |
// Graphics
|
64 |
JPanel panel;
|
65 |
GraphicsConfiguration gc;
|
66 |
volatile BufferedImage image; |
67 |
volatile Graphics2D canvas; |
68 |
int cx, cy;
|
69 |
|
70 |
Socket socket;
|
71 |
OutputStreamWriter out; //TODO: add a BufferedWriter |
72 |
DataUpdater dataupdater; |
73 |
|
74 |
Font botFont;
|
75 |
Random random = new Random(); |
76 |
volatile int tokenLoc; //the token is currently here |
77 |
volatile int numBots; |
78 |
volatile int selectedBot; //the user has selected this bot |
79 |
volatile Rectangle[] botRect; //contains boundary shapes around bots for click detection |
80 |
|
81 |
Thread drawThread;
|
82 |
SelectionIndicator indicator; |
83 |
PacketMonitor packetMonitor; |
84 |
ColonetServerInterface csi; |
85 |
|
86 |
|
87 |
public void init () { |
88 |
// set the default look and feel
|
89 |
String laf = UIManager.getSystemLookAndFeelClassName(); |
90 |
try {
|
91 |
UIManager.setLookAndFeel(laf);
|
92 |
} catch (UnsupportedLookAndFeelException exc) { |
93 |
System.err.println ("Warning: UnsupportedLookAndFeel: " + laf); |
94 |
} catch (Exception exc) { |
95 |
System.err.println ("Error loading " + laf + ": " + exc); |
96 |
} |
97 |
// We should invoke and wait to avoid browser display difficulties
|
98 |
Runnable r = new Runnable() { |
99 |
public void run() { |
100 |
createAndShowGUI(); |
101 |
} |
102 |
}; |
103 |
try {
|
104 |
SwingUtilities.invokeAndWait(r);
|
105 |
} catch (InterruptedException e) { |
106 |
//Not really sure why we would be in this situation
|
107 |
System.out.println(e);
|
108 |
} catch (java.lang.reflect.InvocationTargetException e) {
|
109 |
//This should never happen. Seriously.
|
110 |
System.out.println(e);
|
111 |
} |
112 |
} |
113 |
|
114 |
public void destroy () { |
115 |
try { drawThread.interrupt(); } catch (Exception e) { } |
116 |
try { indicator.interrupt(); } catch (Exception e) { } |
117 |
try { packetMonitor.interrupt(); } catch (Exception e) { } |
118 |
} |
119 |
|
120 |
private synchronized void createAndShowGUI () { |
121 |
// init graphical elements
|
122 |
panel = new JPanel(false); //set automatic double-buffering to false. we are doing it manually. |
123 |
|
124 |
// Connection area
|
125 |
txtInput = new JTextArea("- 9 3 - 1\n- - - 5 -\n4 - - - 2\n- - - - -\n1 - - 3 -"); |
126 |
txtInput.setBorder(BorderFactory.createTitledBorder("Input Matrix")); |
127 |
txtInfo = new JTextArea(); |
128 |
txtInfo.setBorder(BorderFactory.createTitledBorder("Info")); |
129 |
txtInfo.setEditable(false);
|
130 |
btnGraph = new JButton("Run"); |
131 |
txtHost = new JTextField("roboclub1.frc.ri.cmu.edu"); |
132 |
txtHost.setBorder(BorderFactory.createTitledBorder("Host")); |
133 |
txtPort = new JTextField("10123"); |
134 |
txtPort.setBorder(BorderFactory.createTitledBorder("Port")); |
135 |
btnConnect = new JButton("Connect"); |
136 |
lblConnectionStatus = new JLabel("Status: Offline"); |
137 |
panelConnect = new JPanel(); |
138 |
panelConnect.setLayout(new GridLayout(6,1)); |
139 |
panelConnect.add(lblConnectionStatus); |
140 |
panelConnect.add(txtHost); |
141 |
panelConnect.add(txtPort); |
142 |
panelConnect.add(btnConnect); |
143 |
panelConnect.add(txtInfo); |
144 |
panelConnect.add(btnGraph); |
145 |
panelServerInterface = new JPanel(); |
146 |
panelServerInterface.setLayout(new GridLayout(2,1)); |
147 |
panelServerInterface.add(panelConnect); |
148 |
panelServerInterface.add(txtInput); |
149 |
|
150 |
// Status Elements
|
151 |
lblTokenPasses = new JLabel(); |
152 |
lblBattery = new JLabel("???"); |
153 |
panelStats = new JPanel(); |
154 |
panelStats.setLayout(new GridLayout(4,2)); |
155 |
panelStats.add(new JLabel("Token Passes / sec ")); |
156 |
panelStats.add(lblTokenPasses); |
157 |
panelStats.add(new JLabel("Battery ")); |
158 |
panelStats.add(lblBattery); |
159 |
panelStats.add(new JLabel("Token Passes / sec ")); |
160 |
panelStats.add(lblTokenPasses); |
161 |
|
162 |
//TODO: add panelStats somewhere!
|
163 |
|
164 |
// Robot direction panel
|
165 |
panelRobotDirection = new JPanel(); |
166 |
btnF = new JButton("^"); |
167 |
btnB = new JButton("v"); |
168 |
btnL = new JButton("<"); |
169 |
btnR = new JButton(">"); |
170 |
btnActivate = new JButton("o"); |
171 |
panelRobotDirection = new JPanel(); |
172 |
panelRobotDirection.setLayout(new GridLayout(3,3)); |
173 |
panelRobotDirection.add(new JLabel("")); |
174 |
panelRobotDirection.add(btnF); |
175 |
panelRobotDirection.add(new JLabel("")); |
176 |
panelRobotDirection.add(btnL); |
177 |
panelRobotDirection.add(btnActivate); |
178 |
panelRobotDirection.add(btnR); |
179 |
panelRobotDirection.add(new JLabel("")); |
180 |
panelRobotDirection.add(btnB); |
181 |
panelRobotDirection.add(new JLabel("")); |
182 |
|
183 |
// Robot Control and Commands
|
184 |
panelRobotCommands = new JPanel(); |
185 |
panelRobotCommands.setLayout(new FlowLayout()); |
186 |
cmbRobotNum = new JComboBox(); |
187 |
panelRobotCommands.add(cmbRobotNum); |
188 |
panelRobotCommands.add(new JLabel("Commands go here")); |
189 |
panelRobotControl = new JPanel(); |
190 |
panelRobotControl.setLayout(new GridLayout(2,1)); |
191 |
panelRobotControl.add(panelRobotDirection); |
192 |
panelRobotControl.add(panelRobotCommands); |
193 |
|
194 |
// Task Manager
|
195 |
panelTaskManager = new JPanel(); |
196 |
panelTaskManager.setLayout(new BorderLayout()); |
197 |
taskListModel = new DefaultListModel(); |
198 |
taskListModel.addElement("Map the Environment");
|
199 |
taskListModel.addElement("Clean Up Chemical Spill");
|
200 |
taskListModel.addElement("Grow Plants");
|
201 |
taskListModel.addElement("Save the Cheerleader");
|
202 |
taskListModel.addElement("Save the World");
|
203 |
taskList = new JList(taskListModel); |
204 |
taskList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
205 |
taskList.setSelectedIndex(0);
|
206 |
spTaskManager = new JScrollPane(taskList); |
207 |
panelTaskManagerControls = new JPanel(); |
208 |
panelTaskManagerControls.setLayout(new GridLayout(1,3)); |
209 |
panelTaskManagerControlsPriority = new JPanel(); |
210 |
panelTaskManagerControlsPriority.setLayout(new GridLayout(1,2)); |
211 |
btnAddTask = new JButton("Add..."); |
212 |
btnRemoveTask = new JButton("Remove"); |
213 |
btnMoveTaskUp = new JButton("^"); |
214 |
btnMoveTaskDown = new JButton("v"); |
215 |
panelTaskManagerControlsPriority.add(btnMoveTaskUp); |
216 |
panelTaskManagerControlsPriority.add(btnMoveTaskDown); |
217 |
panelTaskManagerControls.add(btnAddTask); |
218 |
panelTaskManagerControls.add(btnRemoveTask); |
219 |
panelTaskManagerControls.add(panelTaskManagerControlsPriority); |
220 |
panelTaskManager.add(spTaskManager, BorderLayout.CENTER);
|
221 |
panelTaskManager.add(panelTaskManagerControls, BorderLayout.SOUTH);
|
222 |
panelTaskManager.add(new JLabel("Current Task Queue"), BorderLayout.NORTH); |
223 |
|
224 |
// Message log
|
225 |
log = new JTextArea(); |
226 |
spLog = new JScrollPane(log, |
227 |
ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
|
228 |
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
|
229 |
spLog.setBorder(BorderFactory.createTitledBorder("Log")); |
230 |
spLog.setPreferredSize(new Dimension(0, 150)); |
231 |
log.setEditable(false);
|
232 |
|
233 |
// Main control mechanism
|
234 |
panelControl = new JPanel(); |
235 |
panelControl.setLayout(new GridLayout(1,1)); |
236 |
tabPaneControl = new JTabbedPane(JTabbedPane.TOP); |
237 |
tabPaneControl.setPreferredSize(new Dimension(300, 0)); |
238 |
tabPaneControl.addTab("Connection", panelServerInterface);
|
239 |
tabPaneControl.addTab("Robots", panelRobotControl);
|
240 |
tabPaneControl.addTab("Tasks", panelTaskManager);
|
241 |
panelControl.add(tabPaneControl); |
242 |
|
243 |
// Set up elements in the south
|
244 |
panelSouth = new JPanel(); |
245 |
panelSouth.setLayout(new GridLayout(1,2)); |
246 |
panelSouth.add(spLog); |
247 |
|
248 |
this.getContentPane().setLayout(new BorderLayout()); |
249 |
this.getContentPane().add(panel, BorderLayout.CENTER); |
250 |
this.getContentPane().add(panelSouth, BorderLayout.SOUTH); |
251 |
this.getContentPane().add(panelControl, BorderLayout.EAST); |
252 |
this.setVisible(true); |
253 |
|
254 |
btnAddTask.addActionListener(this);
|
255 |
btnRemoveTask.addActionListener(this);
|
256 |
btnMoveTaskUp.addActionListener(this);
|
257 |
btnMoveTaskDown.addActionListener(this);
|
258 |
btnGraph.addActionListener(this);
|
259 |
btnConnect.addActionListener(this);
|
260 |
panel.addMouseListener(this);
|
261 |
|
262 |
|
263 |
// Get the graphics configuration of the screen to create a buffer
|
264 |
gc = GraphicsEnvironment.getLocalGraphicsEnvironment()
|
265 |
.getDefaultScreenDevice().getDefaultConfiguration(); |
266 |
image = gc.createCompatibleImage(CANVAS_SIZE,CANVAS_SIZE); |
267 |
canvas = image.createGraphics(); |
268 |
canvas.setStroke(new BasicStroke(2)); //set pen width |
269 |
|
270 |
// Calculate center of canvas
|
271 |
cx = image.getWidth() / 2;
|
272 |
cy = image.getHeight() / 2;
|
273 |
|
274 |
botFont = new Font("Arial", Font.PLAIN, 30); |
275 |
tokenLoc = 0;
|
276 |
numBots = 0;
|
277 |
selectedBot = 0;
|
278 |
|
279 |
// Set up dependent threads
|
280 |
indicator = new SelectionIndicator(canvas);
|
281 |
indicator.setRadius(RADIUS+3, 15); //a tad more than the bot radius |
282 |
packetMonitor = new PacketMonitor();
|
283 |
dataupdater = new DataUpdater();
|
284 |
|
285 |
csi = new ColonetServerInterface(log);
|
286 |
|
287 |
} |
288 |
|
289 |
public synchronized void paint (Graphics g) { |
290 |
/* First, redraw the graphical components in the applet.
|
291 |
This paint method overrides the built-in paint of the
|
292 |
JApplet, and we don't want to deal with redrawing the
|
293 |
components manually. Fuck that shit. */
|
294 |
super.paint(g);
|
295 |
|
296 |
// Place the buffered image on the screen, inside the panel
|
297 |
panel.getGraphics().drawImage(image, 0, 0, Color.WHITE, this); |
298 |
|
299 |
} |
300 |
|
301 |
public synchronized void update (Graphics g) { |
302 |
paint(g); |
303 |
} |
304 |
|
305 |
public void actionPerformed (ActionEvent e) { |
306 |
Object source = e.getSource();
|
307 |
if (source == btnGraph) {
|
308 |
btnGraph.setEnabled(false);
|
309 |
lblConnectionStatus.setText("Simulating");
|
310 |
|
311 |
//Start dependent threads
|
312 |
drawThread = new Thread(this, "drawThread"); |
313 |
drawThread.start(); |
314 |
indicator.start(); |
315 |
packetMonitor.start(); |
316 |
} else if (source == btnConnect) { |
317 |
doSocket(); |
318 |
} else if (source == btnRemoveTask) { |
319 |
try {
|
320 |
taskListModel.remove(taskList.getSelectedIndex()); |
321 |
} catch (ArrayIndexOutOfBoundsException ex) { |
322 |
} |
323 |
} |
324 |
} |
325 |
|
326 |
private void randomize () { |
327 |
Random r = new Random(); |
328 |
StringBuilder s = new StringBuilder(); |
329 |
|
330 |
int count = r.nextInt(8) + 1; |
331 |
for (int i = 0; i < count; i++) { |
332 |
for (int j = 0; j < count; j++) { |
333 |
if (r.nextBoolean())
|
334 |
s.append("" + (r.nextInt(16) + 1)); |
335 |
else
|
336 |
s.append("-");
|
337 |
if (j != count-1) |
338 |
s.append(" ");
|
339 |
} |
340 |
if (i != count-1) s.append("\n"); |
341 |
} |
342 |
|
343 |
txtInput.setText(s.toString()); |
344 |
} |
345 |
|
346 |
private void doSocket () { |
347 |
csi.connect(txtHost.getText(), txtPort.getText()); |
348 |
} |
349 |
|
350 |
public void drawRobot (int id, int x, int y) { |
351 |
//save the bot in memory, so we can tell if we click on it later
|
352 |
botRect[id] = new Rectangle(x-RADIUS, y-RADIUS, 2*RADIUS, 2*RADIUS); |
353 |
|
354 |
//draw the bot on the canvas
|
355 |
canvas.setColor(Color.BLACK);
|
356 |
canvas.drawOval(x-RADIUS, y-RADIUS, RADIUS*2, RADIUS*2); |
357 |
|
358 |
//draw the label
|
359 |
canvas.setFont(botFont); |
360 |
canvas.drawString("" + id, x-10, y+10); |
361 |
} |
362 |
|
363 |
public void drawConnection (int start, int end, int radius, Color color) { |
364 |
final int ARROW_LENGTH = 18; |
365 |
|
366 |
double angle = 2.0 * Math.PI / numBots; |
367 |
int startx, starty, endx, endy;
|
368 |
startx = cx - (int)(radius * Math.cos(start * angle)); |
369 |
starty = cy - (int)(radius * Math.sin(start * angle)); |
370 |
endx = cx - (int)(radius * Math.cos(end * angle)); |
371 |
endy = cy - (int)(radius * Math.sin(end * angle)); |
372 |
canvas.setColor(color); |
373 |
canvas.drawLine(startx, starty, endx, endy); |
374 |
|
375 |
//create arrow
|
376 |
if (color.equals(Color.BLACK)) return; |
377 |
int big_dy = starty - endy;
|
378 |
int big_dx = endx - startx;
|
379 |
double theta = 0; |
380 |
if (big_dx == 0 && starty > endy) //pointing up |
381 |
theta = Math.PI/2; |
382 |
else if (big_dx == 0 && starty < endy) //pointing down |
383 |
theta = 3*Math.PI/2; |
384 |
else if (big_dy == 0 && startx > endx) //pointing left |
385 |
theta = Math.PI;
|
386 |
else if (big_dy == 0 && startx < endx) //pointing right |
387 |
theta = 0;
|
388 |
else
|
389 |
theta = Math.atan(1.0 * big_dy / big_dx); |
390 |
|
391 |
//create ploygon
|
392 |
Polygon poly = new Polygon(); |
393 |
int dx_arrow = Math.abs((int)(ARROW_LENGTH * Math.cos(theta))); |
394 |
int dy_arrow = Math.abs((int)(ARROW_LENGTH * Math.sin(theta))); |
395 |
int dy_half = (int)(ARROW_LENGTH/2 * Math.cos(theta)); |
396 |
int dx_half = (int)(ARROW_LENGTH/2 * Math.sin(theta)); |
397 |
int rx = (big_dx > 0) ? endx - dx_arrow : endx + dx_arrow; |
398 |
int ry = (big_dy > 0) ? endy + dy_arrow : endy - dy_arrow; |
399 |
poly.addPoint(endx, endy); |
400 |
poly.addPoint(rx - dx_half, ry - dy_half); |
401 |
poly.addPoint(rx + dx_half, ry + dy_half); |
402 |
canvas.fillPolygon(poly); |
403 |
} |
404 |
|
405 |
public void run () { |
406 |
while (true) { |
407 |
step(); |
408 |
repaint(); |
409 |
try {
|
410 |
Thread.sleep(90); |
411 |
} catch (InterruptedException e) { |
412 |
return;
|
413 |
} |
414 |
} |
415 |
} |
416 |
|
417 |
public void step () { |
418 |
final int DIAMETER = image.getWidth() - 2*BUFFER; |
419 |
final int BIGRADIUS = DIAMETER / 2; |
420 |
final int TOKENRADIUS = 40; |
421 |
boolean valid;
|
422 |
|
423 |
// clear image
|
424 |
canvas.setColor(Color.WHITE);
|
425 |
canvas.fillRect(0, 0, image.getWidth(), image.getHeight()); |
426 |
|
427 |
// parse the matrix, to see what robots exist
|
428 |
String [] rows = txtInput.getText().split("\n"); |
429 |
numBots = rows.length; |
430 |
String [][] entries = new String[numBots][numBots]; |
431 |
valid = true;
|
432 |
for (int i = 0; i < numBots; i++) { |
433 |
entries[i] = rows[i].split(" ");
|
434 |
if (entries[i].length != rows.length) valid = false; |
435 |
} |
436 |
|
437 |
if (valid) {
|
438 |
this.showStatus("Running"); |
439 |
|
440 |
// draw robots and find which one is seleced
|
441 |
double angle = 2.0 * Math.PI / numBots; |
442 |
canvas.setColor(Color.BLACK);
|
443 |
botRect = new Rectangle[numBots]; |
444 |
int x, y;
|
445 |
if (selectedBot >= numBots) selectedBot = 0; |
446 |
for (int i = 0; i < numBots; i++) { |
447 |
x = cx - (int)(BIGRADIUS * Math.cos(i * angle)); |
448 |
y = cy - (int)(BIGRADIUS * Math.sin(i * angle)); |
449 |
drawRobot(i, x, y); |
450 |
if (i == selectedBot) indicator.setCenter(x, y);
|
451 |
} |
452 |
|
453 |
// draw token marker
|
454 |
int tokenx, tokeny;
|
455 |
int tokenNum = tokenLoc;
|
456 |
tokenx = cx - (int)(BIGRADIUS * Math.cos(tokenNum * angle)); |
457 |
tokeny = cy - (int)(BIGRADIUS * Math.sin(tokenNum * angle)); |
458 |
canvas.setColor(Color.RED);
|
459 |
canvas.drawOval(tokenx-TOKENRADIUS, tokeny-TOKENRADIUS, 2*TOKENRADIUS, 2*TOKENRADIUS); |
460 |
|
461 |
// create an inner circle along which the connections are made.
|
462 |
// let the diameter of this circle be 2*RADIUS less than the outerDiameter.
|
463 |
// see what connections exist
|
464 |
for (int row = 0; row < numBots; row++) { |
465 |
for(int col = 0; col < numBots; col++) { |
466 |
if (!entries[row][col].equals("-") && entries[col][row].equals("-") && row != col) { |
467 |
//TODO: Make a standard gray
|
468 |
drawConnection(row, col, BIGRADIUS-RADIUS, new Color(200,200,200)); |
469 |
} else if (!entries[row][col].equals("-") && ! entries[col][row].equals("-") && row != col) { |
470 |
drawConnection(row, col, BIGRADIUS-RADIUS, Color.BLACK);
|
471 |
} |
472 |
} |
473 |
} |
474 |
|
475 |
// draw the selection indicator
|
476 |
indicator.draw(); |
477 |
|
478 |
} else {// if matrix is not valid |
479 |
this.showStatus("Error: Invalid matrix"); |
480 |
} |
481 |
|
482 |
} |
483 |
|
484 |
/* At this point, moveToken is only called by the simulator.
|
485 |
* In the future, it can be rewritten to account for non-standard
|
486 |
* token passing or deleted if the information can be retrieved
|
487 |
* directly from the Colonet server instead.
|
488 |
*/
|
489 |
public void moveToken () { |
490 |
try {
|
491 |
tokenLoc = (tokenLoc+1)%numBots;
|
492 |
} catch (ArithmeticException e) { // in case numRobots is zero |
493 |
} |
494 |
|
495 |
packetMonitor.addTokenPass(); |
496 |
} |
497 |
|
498 |
//
|
499 |
// MouseEvent methods
|
500 |
//
|
501 |
public void mouseExited(MouseEvent e) {} |
502 |
public void mouseEntered(MouseEvent e) {} |
503 |
public void mouseReleased(MouseEvent e) {} |
504 |
public void mouseClicked(MouseEvent e) {} |
505 |
public void mousePressed(MouseEvent e) { |
506 |
try {
|
507 |
for (int i = 0; i < numBots; i++) { |
508 |
if (botRect[i].contains(e.getPoint()))
|
509 |
selectedBot = i; |
510 |
} |
511 |
} catch (Exception ex) { |
512 |
System.out.println(e);
|
513 |
} |
514 |
|
515 |
} |
516 |
|
517 |
/*
|
518 |
* SelectionIndicator thread.
|
519 |
* Graphical representation of the selection marker
|
520 |
*
|
521 |
* step() and draw() are synchronized methods. step() is private and
|
522 |
* used to update the position of the crosshairs. draw() is called
|
523 |
* externally and should only run if all calculations in step() have
|
524 |
* been completed.
|
525 |
*/
|
526 |
private class SelectionIndicator extends Thread { |
527 |
|
528 |
final int INDICATOR_DELAY = 100; |
529 |
final double DTHETA = 0.3; //larger values make the marker rotate faster |
530 |
Graphics2D g; //canvas to draw on |
531 |
boolean running;
|
532 |
|
533 |
int sx, sy; //center |
534 |
int r, dr; //radius and width of marker |
535 |
double theta; //current angle |
536 |
|
537 |
volatile Polygon poly1, poly2, poly3, poly4; |
538 |
|
539 |
int px1, py1;
|
540 |
int rx1, ry1;
|
541 |
int px2, py2;
|
542 |
int rx2, ry2;
|
543 |
int px3, py3;
|
544 |
int rx3, ry3;
|
545 |
int px4, py4;
|
546 |
int rx4, ry4;
|
547 |
|
548 |
int steps;
|
549 |
|
550 |
public SelectionIndicator (Graphics2D g) { |
551 |
super("SelectionIndicator"); |
552 |
this.g = g;
|
553 |
running = false;
|
554 |
steps = 0;
|
555 |
|
556 |
theta = 0;
|
557 |
rx1 = 0; ry1 = 0; |
558 |
px1 = 0; py1 = 0; |
559 |
rx2 = 0; ry2 = 0; |
560 |
px2 = 0; py2 = 0; |
561 |
rx3 = 0; ry3 = 0; |
562 |
px3 = 0; py3 = 0; |
563 |
rx4 = 0; ry4 = 0; |
564 |
px4 = 0; py4 = 0; |
565 |
} |
566 |
|
567 |
public synchronized void setCenter (int sx, int sy) { |
568 |
if (sx == this.sx && sy == this.sy) return; |
569 |
this.sx = sx;
|
570 |
this.sy = sy;
|
571 |
steps = 0;
|
572 |
} |
573 |
|
574 |
public synchronized void setRadius (int r, int dr) { |
575 |
this.r = r;
|
576 |
this.dr = dr;
|
577 |
steps = 0;
|
578 |
} |
579 |
|
580 |
public void run () { |
581 |
running = true;
|
582 |
while (running) {
|
583 |
step(); |
584 |
try {
|
585 |
Thread.sleep(INDICATOR_DELAY);
|
586 |
} catch (InterruptedException e) { |
587 |
running = false;
|
588 |
return;
|
589 |
} |
590 |
} |
591 |
} |
592 |
|
593 |
private synchronized void step () { |
594 |
Polygon poly1_new = new Polygon(); |
595 |
Polygon poly2_new = new Polygon(); |
596 |
Polygon poly3_new = new Polygon(); |
597 |
Polygon poly4_new = new Polygon(); |
598 |
|
599 |
//the step
|
600 |
theta = (theta + DTHETA/Math.PI) % (Math.PI); |
601 |
|
602 |
//the calculation
|
603 |
//let p be the point of the pointy thing toward the center
|
604 |
//let r be the point at the opposite side
|
605 |
|
606 |
//recalculate radius, if it will look cool, lolz
|
607 |
int newr = r;
|
608 |
if (steps < 100) |
609 |
newr = (int)( r + 200/(steps+1) ); |
610 |
|
611 |
//precompute values for dx and dy
|
612 |
int dx_inner = (int)(newr * Math.cos(theta)); |
613 |
int dy_inner = (int)(newr * Math.sin(theta)); |
614 |
int dx_outer = (int)((newr+dr) * Math.cos(theta)); |
615 |
int dy_outer = (int)((newr+dr) * Math.sin(theta)); |
616 |
|
617 |
//calculate polygon constants
|
618 |
int dy_poly = (int)(dr/2 * Math.cos(theta)); |
619 |
int dx_poly = (int)(dr/2 * Math.sin(theta)); |
620 |
|
621 |
//determine critical points
|
622 |
//kansas city shuffle!
|
623 |
px1 = sx + dx_inner; |
624 |
py1 = sy - dy_inner; |
625 |
rx1 = sx + dx_outer; |
626 |
ry1 = sy - dy_outer; |
627 |
px2 = sx - dx_inner; |
628 |
py2 = sy + dy_inner; |
629 |
rx2 = sx - dx_outer; |
630 |
ry2 = sy + dy_outer; |
631 |
px3 = sx - dy_inner; |
632 |
py3 = sy - dx_inner; |
633 |
rx3 = sx - dy_outer; |
634 |
ry3 = sy - dx_outer; |
635 |
px4 = sx + dy_inner; |
636 |
py4 = sy + dx_inner; |
637 |
rx4 = sx + dy_outer; |
638 |
ry4 = sy + dx_outer; |
639 |
|
640 |
//create polygons
|
641 |
poly1_new.addPoint(px1, py1); |
642 |
poly1_new.addPoint(rx1+dx_poly, ry1+dy_poly); |
643 |
poly1_new.addPoint(rx1-dx_poly, ry1-dy_poly); |
644 |
poly2_new.addPoint(px2, py2); |
645 |
poly2_new.addPoint(rx2+dx_poly, ry2+dy_poly); |
646 |
poly2_new.addPoint(rx2-dx_poly, ry2-dy_poly); |
647 |
poly3_new.addPoint(px3, py3); |
648 |
poly3_new.addPoint(rx3-dy_poly, ry3+dx_poly); |
649 |
poly3_new.addPoint(rx3+dy_poly, ry3-dx_poly); |
650 |
poly4_new.addPoint(px4, py4); |
651 |
poly4_new.addPoint(rx4-dy_poly, ry4+dx_poly); |
652 |
poly4_new.addPoint(rx4+dy_poly, ry4-dx_poly); |
653 |
|
654 |
//reassign updated polygons
|
655 |
poly1 = poly1_new; |
656 |
poly2 = poly2_new; |
657 |
poly3 = poly3_new; |
658 |
poly4 = poly4_new; |
659 |
|
660 |
if (steps < 300) steps++; |
661 |
} |
662 |
|
663 |
public synchronized void draw () { |
664 |
if (!running) return; |
665 |
g.setColor(Color.GRAY);
|
666 |
//draw polygons
|
667 |
g.fillPolygon(poly1); |
668 |
g.fillPolygon(poly2); |
669 |
g.fillPolygon(poly3); |
670 |
g.fillPolygon(poly4); |
671 |
} |
672 |
|
673 |
} |
674 |
|
675 |
/*
|
676 |
* Simulator thread.
|
677 |
*
|
678 |
*/
|
679 |
private class Simulator extends Thread { |
680 |
final int SIMULATOR_DELAY = 300; |
681 |
boolean running;
|
682 |
|
683 |
public Simulator () {
|
684 |
super("Simulator"); |
685 |
running = false;
|
686 |
} |
687 |
|
688 |
public void run () { |
689 |
running = true;
|
690 |
while (running) {
|
691 |
step(); |
692 |
try {
|
693 |
Thread.sleep(SIMULATOR_DELAY);
|
694 |
} catch (InterruptedException e) { |
695 |
running = false;
|
696 |
return;
|
697 |
} |
698 |
} |
699 |
} |
700 |
|
701 |
private void step () { |
702 |
// simulate passing the token
|
703 |
moveToken(); |
704 |
} |
705 |
|
706 |
} |
707 |
|
708 |
/*
|
709 |
* PacketMonitor thread.
|
710 |
*
|
711 |
* Currently, this counts the rate of token passes but will eventually
|
712 |
* be modified to keep more important statistics.
|
713 |
*/
|
714 |
private class PacketMonitor extends Thread { |
715 |
final int PACKETMONITOR_DELAY = 1000; |
716 |
|
717 |
boolean running;
|
718 |
int tokenPasses;
|
719 |
|
720 |
public PacketMonitor () {
|
721 |
super("PacketMonitor"); |
722 |
running = false;
|
723 |
tokenPasses = 0;
|
724 |
} |
725 |
|
726 |
public void run () { |
727 |
running = true;
|
728 |
while (running) {
|
729 |
displayTokenPasses(); |
730 |
try {
|
731 |
Thread.sleep(PACKETMONITOR_DELAY);
|
732 |
} catch (InterruptedException e) { |
733 |
running = false;
|
734 |
return;
|
735 |
} |
736 |
} |
737 |
} |
738 |
|
739 |
public synchronized void addTokenPass () { |
740 |
tokenPasses++; |
741 |
} |
742 |
|
743 |
public synchronized void displayTokenPasses () { |
744 |
lblTokenPasses.setText("" + tokenPasses);
|
745 |
tokenPasses = 0;
|
746 |
} |
747 |
|
748 |
} |
749 |
|
750 |
/*
|
751 |
* DataUpdater thread.
|
752 |
*
|
753 |
*/
|
754 |
class DataUpdater extends Thread { |
755 |
final int DATAUPDATER_DELAY = 666; |
756 |
|
757 |
public DataUpdater () {
|
758 |
super("Colonet DataUpdater"); |
759 |
} |
760 |
|
761 |
public void run () { |
762 |
String line;
|
763 |
while (true) { |
764 |
try {
|
765 |
if (csi == null) return; |
766 |
line = csi.getLine(); |
767 |
if (line != null) { |
768 |
log.append("Incoming data: [" + line + "]"); |
769 |
//TODO: parse incoming data here
|
770 |
|
771 |
//request more data
|
772 |
//csi.sendRequest("Get all data", "server");
|
773 |
|
774 |
// update robot list
|
775 |
int numRobots = 5; |
776 |
String [] robotList = new String[numRobots+1]; |
777 |
robotList[0] = "All"; |
778 |
for (int i = 1; i <= numRobots; i++) |
779 |
robotList[i] = "Robot " + i;
|
780 |
cmbRobotNum = new JComboBox(robotList); |
781 |
} |
782 |
Thread.sleep(DATAUPDATER_DELAY);
|
783 |
} catch (InterruptedException e) { |
784 |
return;
|
785 |
} |
786 |
} |
787 |
} |
788 |
|
789 |
} |
790 |
|
791 |
} |