root / trunk / code / projects / colonet / ColonetGUI / Colonet.java @ 72
History | View | Annotate | Download (23.3 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 |
/* Add all listeners here */
|
255 |
// Task Management
|
256 |
btnAddTask.addActionListener(this);
|
257 |
btnRemoveTask.addActionListener(this);
|
258 |
btnMoveTaskUp.addActionListener(this);
|
259 |
btnMoveTaskDown.addActionListener(this);
|
260 |
// Robot Control
|
261 |
btnF.addActionListener(this);
|
262 |
btnB.addActionListener(this);
|
263 |
btnL.addActionListener(this);
|
264 |
btnR.addActionListener(this);
|
265 |
btnActivate.addActionListener(this);
|
266 |
// Other
|
267 |
btnGraph.addActionListener(this);
|
268 |
btnConnect.addActionListener(this);
|
269 |
panel.addMouseListener(this);
|
270 |
|
271 |
|
272 |
// Get the graphics configuration of the screen to create a buffer
|
273 |
gc = GraphicsEnvironment.getLocalGraphicsEnvironment()
|
274 |
.getDefaultScreenDevice().getDefaultConfiguration(); |
275 |
image = gc.createCompatibleImage(CANVAS_SIZE,CANVAS_SIZE); |
276 |
canvas = image.createGraphics(); |
277 |
canvas.setStroke(new BasicStroke(2)); //set pen width |
278 |
|
279 |
// Calculate center of canvas
|
280 |
cx = image.getWidth() / 2;
|
281 |
cy = image.getHeight() / 2;
|
282 |
|
283 |
botFont = new Font("Arial", Font.PLAIN, 30); |
284 |
tokenLoc = 0;
|
285 |
numBots = 0;
|
286 |
selectedBot = 0;
|
287 |
|
288 |
// Set up dependent threads
|
289 |
indicator = new SelectionIndicator(canvas);
|
290 |
indicator.setRadius(RADIUS+3, 15); //a tad more than the bot radius |
291 |
packetMonitor = new PacketMonitor();
|
292 |
dataupdater = new DataUpdater();
|
293 |
|
294 |
csi = new ColonetServerInterface(log);
|
295 |
|
296 |
} |
297 |
|
298 |
public synchronized void paint (Graphics g) { |
299 |
/* First, redraw the graphical components in the applet.
|
300 |
This paint method overrides the built-in paint of the
|
301 |
JApplet, and we don't want to deal with redrawing the
|
302 |
components manually. Fuck that shit. */
|
303 |
super.paint(g);
|
304 |
|
305 |
// Place the buffered image on the screen, inside the panel
|
306 |
panel.getGraphics().drawImage(image, 0, 0, Color.WHITE, this); |
307 |
|
308 |
} |
309 |
|
310 |
public synchronized void update (Graphics g) { |
311 |
paint(g); |
312 |
} |
313 |
|
314 |
public void actionPerformed (ActionEvent e) { |
315 |
Object source = e.getSource();
|
316 |
if (source == btnGraph) {
|
317 |
btnGraph.setEnabled(false);
|
318 |
lblConnectionStatus.setText("Simulating");
|
319 |
|
320 |
//Start dependent threads
|
321 |
drawThread = new Thread(this, "drawThread"); |
322 |
drawThread.start(); |
323 |
indicator.start(); |
324 |
packetMonitor.start(); |
325 |
} else if (source == btnConnect) { |
326 |
doSocket(); |
327 |
} else if (source == btnRemoveTask) { |
328 |
try {
|
329 |
taskListModel.remove(taskList.getSelectedIndex()); |
330 |
} catch (ArrayIndexOutOfBoundsException ex) { |
331 |
} |
332 |
|
333 |
// Robot controls
|
334 |
} else if (source == btnF) { |
335 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " 0 200", ColonetServerInterface.GLOBAL_DEST);
|
336 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " 0 200", ColonetServerInterface.GLOBAL_DEST);
|
337 |
} else if (source == btnB) { |
338 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " 1 200", ColonetServerInterface.GLOBAL_DEST);
|
339 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " 1 200", ColonetServerInterface.GLOBAL_DEST);
|
340 |
} else if (source == btnL) { |
341 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " 1 200", ColonetServerInterface.GLOBAL_DEST);
|
342 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " 0 200", ColonetServerInterface.GLOBAL_DEST);
|
343 |
} else if (source == btnR) { |
344 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " 0 200", ColonetServerInterface.GLOBAL_DEST);
|
345 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " 1 200", ColonetServerInterface.GLOBAL_DEST);
|
346 |
} else if (source == btnActivate) { |
347 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " 0 0", ColonetServerInterface.GLOBAL_DEST);
|
348 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " 0 0", ColonetServerInterface.GLOBAL_DEST);
|
349 |
} |
350 |
} |
351 |
|
352 |
private void randomize () { |
353 |
Random r = new Random(); |
354 |
StringBuilder s = new StringBuilder(); |
355 |
|
356 |
int count = r.nextInt(8) + 1; |
357 |
for (int i = 0; i < count; i++) { |
358 |
for (int j = 0; j < count; j++) { |
359 |
if (r.nextBoolean())
|
360 |
s.append("" + (r.nextInt(16) + 1)); |
361 |
else
|
362 |
s.append("-");
|
363 |
if (j != count-1) |
364 |
s.append(" ");
|
365 |
} |
366 |
if (i != count-1) s.append("\n"); |
367 |
} |
368 |
|
369 |
txtInput.setText(s.toString()); |
370 |
} |
371 |
|
372 |
private void doSocket () { |
373 |
csi.connect(txtHost.getText(), txtPort.getText()); |
374 |
} |
375 |
|
376 |
public void drawRobot (int id, int x, int y) { |
377 |
//save the bot in memory, so we can tell if we click on it later
|
378 |
botRect[id] = new Rectangle(x-RADIUS, y-RADIUS, 2*RADIUS, 2*RADIUS); |
379 |
|
380 |
//draw the bot on the canvas
|
381 |
canvas.setColor(Color.BLACK);
|
382 |
canvas.drawOval(x-RADIUS, y-RADIUS, RADIUS*2, RADIUS*2); |
383 |
|
384 |
//draw the label
|
385 |
canvas.setFont(botFont); |
386 |
canvas.drawString("" + id, x-10, y+10); |
387 |
} |
388 |
|
389 |
public void drawConnection (int start, int end, int radius, Color color) { |
390 |
final int ARROW_LENGTH = 18; |
391 |
|
392 |
double angle = 2.0 * Math.PI / numBots; |
393 |
int startx, starty, endx, endy;
|
394 |
startx = cx - (int)(radius * Math.cos(start * angle)); |
395 |
starty = cy - (int)(radius * Math.sin(start * angle)); |
396 |
endx = cx - (int)(radius * Math.cos(end * angle)); |
397 |
endy = cy - (int)(radius * Math.sin(end * angle)); |
398 |
canvas.setColor(color); |
399 |
canvas.drawLine(startx, starty, endx, endy); |
400 |
|
401 |
//create arrow
|
402 |
if (color.equals(Color.BLACK)) return; |
403 |
int big_dy = starty - endy;
|
404 |
int big_dx = endx - startx;
|
405 |
double theta = 0; |
406 |
if (big_dx == 0 && starty > endy) //pointing up |
407 |
theta = Math.PI/2; |
408 |
else if (big_dx == 0 && starty < endy) //pointing down |
409 |
theta = 3*Math.PI/2; |
410 |
else if (big_dy == 0 && startx > endx) //pointing left |
411 |
theta = Math.PI;
|
412 |
else if (big_dy == 0 && startx < endx) //pointing right |
413 |
theta = 0;
|
414 |
else
|
415 |
theta = Math.atan(1.0 * big_dy / big_dx); |
416 |
|
417 |
//create ploygon
|
418 |
Polygon poly = new Polygon(); |
419 |
int dx_arrow = Math.abs((int)(ARROW_LENGTH * Math.cos(theta))); |
420 |
int dy_arrow = Math.abs((int)(ARROW_LENGTH * Math.sin(theta))); |
421 |
int dy_half = (int)(ARROW_LENGTH/2 * Math.cos(theta)); |
422 |
int dx_half = (int)(ARROW_LENGTH/2 * Math.sin(theta)); |
423 |
int rx = (big_dx > 0) ? endx - dx_arrow : endx + dx_arrow; |
424 |
int ry = (big_dy > 0) ? endy + dy_arrow : endy - dy_arrow; |
425 |
poly.addPoint(endx, endy); |
426 |
poly.addPoint(rx - dx_half, ry - dy_half); |
427 |
poly.addPoint(rx + dx_half, ry + dy_half); |
428 |
canvas.fillPolygon(poly); |
429 |
} |
430 |
|
431 |
public void run () { |
432 |
while (true) { |
433 |
step(); |
434 |
repaint(); |
435 |
try {
|
436 |
Thread.sleep(90); |
437 |
} catch (InterruptedException e) { |
438 |
return;
|
439 |
} |
440 |
} |
441 |
} |
442 |
|
443 |
public void step () { |
444 |
final int DIAMETER = image.getWidth() - 2*BUFFER; |
445 |
final int BIGRADIUS = DIAMETER / 2; |
446 |
final int TOKENRADIUS = 40; |
447 |
boolean valid;
|
448 |
|
449 |
// clear image
|
450 |
canvas.setColor(Color.WHITE);
|
451 |
canvas.fillRect(0, 0, image.getWidth(), image.getHeight()); |
452 |
|
453 |
// parse the matrix, to see what robots exist
|
454 |
String [] rows = txtInput.getText().split("\n"); |
455 |
numBots = rows.length; |
456 |
String [][] entries = new String[numBots][numBots]; |
457 |
valid = true;
|
458 |
for (int i = 0; i < numBots; i++) { |
459 |
entries[i] = rows[i].split(" ");
|
460 |
if (entries[i].length != rows.length) valid = false; |
461 |
} |
462 |
|
463 |
if (valid) {
|
464 |
this.showStatus("Running"); |
465 |
|
466 |
// draw robots and find which one is seleced
|
467 |
double angle = 2.0 * Math.PI / numBots; |
468 |
canvas.setColor(Color.BLACK);
|
469 |
botRect = new Rectangle[numBots]; |
470 |
int x, y;
|
471 |
if (selectedBot >= numBots) selectedBot = 0; |
472 |
for (int i = 0; i < numBots; i++) { |
473 |
x = cx - (int)(BIGRADIUS * Math.cos(i * angle)); |
474 |
y = cy - (int)(BIGRADIUS * Math.sin(i * angle)); |
475 |
drawRobot(i, x, y); |
476 |
if (i == selectedBot) indicator.setCenter(x, y);
|
477 |
} |
478 |
|
479 |
// draw token marker
|
480 |
int tokenx, tokeny;
|
481 |
int tokenNum = tokenLoc;
|
482 |
tokenx = cx - (int)(BIGRADIUS * Math.cos(tokenNum * angle)); |
483 |
tokeny = cy - (int)(BIGRADIUS * Math.sin(tokenNum * angle)); |
484 |
canvas.setColor(Color.RED);
|
485 |
canvas.drawOval(tokenx-TOKENRADIUS, tokeny-TOKENRADIUS, 2*TOKENRADIUS, 2*TOKENRADIUS); |
486 |
|
487 |
// create an inner circle along which the connections are made.
|
488 |
// let the diameter of this circle be 2*RADIUS less than the outerDiameter.
|
489 |
// see what connections exist
|
490 |
for (int row = 0; row < numBots; row++) { |
491 |
for(int col = 0; col < numBots; col++) { |
492 |
if (!entries[row][col].equals("-") && entries[col][row].equals("-") && row != col) { |
493 |
//TODO: Make a standard gray
|
494 |
drawConnection(row, col, BIGRADIUS-RADIUS, new Color(200,200,200)); |
495 |
} else if (!entries[row][col].equals("-") && ! entries[col][row].equals("-") && row != col) { |
496 |
drawConnection(row, col, BIGRADIUS-RADIUS, Color.BLACK);
|
497 |
} |
498 |
} |
499 |
} |
500 |
|
501 |
// draw the selection indicator
|
502 |
indicator.draw(); |
503 |
|
504 |
} else {// if matrix is not valid |
505 |
this.showStatus("Error: Invalid matrix"); |
506 |
} |
507 |
|
508 |
} |
509 |
|
510 |
/* At this point, moveToken is only called by the simulator.
|
511 |
* In the future, it can be rewritten to account for non-standard
|
512 |
* token passing or deleted if the information can be retrieved
|
513 |
* directly from the Colonet server instead.
|
514 |
*/
|
515 |
public void moveToken () { |
516 |
try {
|
517 |
tokenLoc = (tokenLoc+1)%numBots;
|
518 |
} catch (ArithmeticException e) { // in case numRobots is zero |
519 |
} |
520 |
|
521 |
packetMonitor.addTokenPass(); |
522 |
} |
523 |
|
524 |
//
|
525 |
// MouseEvent methods
|
526 |
//
|
527 |
public void mouseExited(MouseEvent e) {} |
528 |
public void mouseEntered(MouseEvent e) {} |
529 |
public void mouseReleased(MouseEvent e) {} |
530 |
public void mouseClicked(MouseEvent e) {} |
531 |
public void mousePressed(MouseEvent e) { |
532 |
try {
|
533 |
for (int i = 0; i < numBots; i++) { |
534 |
if (botRect[i].contains(e.getPoint()))
|
535 |
selectedBot = i; |
536 |
} |
537 |
} catch (Exception ex) { |
538 |
System.out.println(e);
|
539 |
} |
540 |
|
541 |
} |
542 |
|
543 |
/*
|
544 |
* SelectionIndicator thread.
|
545 |
* Graphical representation of the selection marker
|
546 |
*
|
547 |
* step() and draw() are synchronized methods. step() is private and
|
548 |
* used to update the position of the crosshairs. draw() is called
|
549 |
* externally and should only run if all calculations in step() have
|
550 |
* been completed.
|
551 |
*/
|
552 |
private class SelectionIndicator extends Thread { |
553 |
|
554 |
final int INDICATOR_DELAY = 100; |
555 |
final double DTHETA = 0.3; //larger values make the marker rotate faster |
556 |
Graphics2D g; //canvas to draw on |
557 |
boolean running;
|
558 |
|
559 |
int sx, sy; //center |
560 |
int r, dr; //radius and width of marker |
561 |
double theta; //current angle |
562 |
|
563 |
volatile Polygon poly1, poly2, poly3, poly4; |
564 |
|
565 |
int px1, py1;
|
566 |
int rx1, ry1;
|
567 |
int px2, py2;
|
568 |
int rx2, ry2;
|
569 |
int px3, py3;
|
570 |
int rx3, ry3;
|
571 |
int px4, py4;
|
572 |
int rx4, ry4;
|
573 |
|
574 |
int steps;
|
575 |
|
576 |
public SelectionIndicator (Graphics2D g) { |
577 |
super("SelectionIndicator"); |
578 |
this.g = g;
|
579 |
running = false;
|
580 |
steps = 0;
|
581 |
|
582 |
theta = 0;
|
583 |
rx1 = 0; ry1 = 0; |
584 |
px1 = 0; py1 = 0; |
585 |
rx2 = 0; ry2 = 0; |
586 |
px2 = 0; py2 = 0; |
587 |
rx3 = 0; ry3 = 0; |
588 |
px3 = 0; py3 = 0; |
589 |
rx4 = 0; ry4 = 0; |
590 |
px4 = 0; py4 = 0; |
591 |
} |
592 |
|
593 |
public synchronized void setCenter (int sx, int sy) { |
594 |
if (sx == this.sx && sy == this.sy) return; |
595 |
this.sx = sx;
|
596 |
this.sy = sy;
|
597 |
steps = 0;
|
598 |
} |
599 |
|
600 |
public synchronized void setRadius (int r, int dr) { |
601 |
this.r = r;
|
602 |
this.dr = dr;
|
603 |
steps = 0;
|
604 |
} |
605 |
|
606 |
public void run () { |
607 |
running = true;
|
608 |
while (running) {
|
609 |
step(); |
610 |
try {
|
611 |
Thread.sleep(INDICATOR_DELAY);
|
612 |
} catch (InterruptedException e) { |
613 |
running = false;
|
614 |
return;
|
615 |
} |
616 |
} |
617 |
} |
618 |
|
619 |
private synchronized void step () { |
620 |
Polygon poly1_new = new Polygon(); |
621 |
Polygon poly2_new = new Polygon(); |
622 |
Polygon poly3_new = new Polygon(); |
623 |
Polygon poly4_new = new Polygon(); |
624 |
|
625 |
//the step
|
626 |
theta = (theta + DTHETA/Math.PI) % (Math.PI); |
627 |
|
628 |
//the calculation
|
629 |
//let p be the point of the pointy thing toward the center
|
630 |
//let r be the point at the opposite side
|
631 |
|
632 |
//recalculate radius, if it will look cool, lolz
|
633 |
int newr = r;
|
634 |
if (steps < 100) |
635 |
newr = (int)( r + 200/(steps+1) ); |
636 |
|
637 |
//precompute values for dx and dy
|
638 |
int dx_inner = (int)(newr * Math.cos(theta)); |
639 |
int dy_inner = (int)(newr * Math.sin(theta)); |
640 |
int dx_outer = (int)((newr+dr) * Math.cos(theta)); |
641 |
int dy_outer = (int)((newr+dr) * Math.sin(theta)); |
642 |
|
643 |
//calculate polygon constants
|
644 |
int dy_poly = (int)(dr/2 * Math.cos(theta)); |
645 |
int dx_poly = (int)(dr/2 * Math.sin(theta)); |
646 |
|
647 |
//determine critical points
|
648 |
//kansas city shuffle!
|
649 |
px1 = sx + dx_inner; |
650 |
py1 = sy - dy_inner; |
651 |
rx1 = sx + dx_outer; |
652 |
ry1 = sy - dy_outer; |
653 |
px2 = sx - dx_inner; |
654 |
py2 = sy + dy_inner; |
655 |
rx2 = sx - dx_outer; |
656 |
ry2 = sy + dy_outer; |
657 |
px3 = sx - dy_inner; |
658 |
py3 = sy - dx_inner; |
659 |
rx3 = sx - dy_outer; |
660 |
ry3 = sy - dx_outer; |
661 |
px4 = sx + dy_inner; |
662 |
py4 = sy + dx_inner; |
663 |
rx4 = sx + dy_outer; |
664 |
ry4 = sy + dx_outer; |
665 |
|
666 |
//create polygons
|
667 |
poly1_new.addPoint(px1, py1); |
668 |
poly1_new.addPoint(rx1+dx_poly, ry1+dy_poly); |
669 |
poly1_new.addPoint(rx1-dx_poly, ry1-dy_poly); |
670 |
poly2_new.addPoint(px2, py2); |
671 |
poly2_new.addPoint(rx2+dx_poly, ry2+dy_poly); |
672 |
poly2_new.addPoint(rx2-dx_poly, ry2-dy_poly); |
673 |
poly3_new.addPoint(px3, py3); |
674 |
poly3_new.addPoint(rx3-dy_poly, ry3+dx_poly); |
675 |
poly3_new.addPoint(rx3+dy_poly, ry3-dx_poly); |
676 |
poly4_new.addPoint(px4, py4); |
677 |
poly4_new.addPoint(rx4-dy_poly, ry4+dx_poly); |
678 |
poly4_new.addPoint(rx4+dy_poly, ry4-dx_poly); |
679 |
|
680 |
//reassign updated polygons
|
681 |
poly1 = poly1_new; |
682 |
poly2 = poly2_new; |
683 |
poly3 = poly3_new; |
684 |
poly4 = poly4_new; |
685 |
|
686 |
if (steps < 300) steps++; |
687 |
} |
688 |
|
689 |
public synchronized void draw () { |
690 |
if (!running) return; |
691 |
g.setColor(Color.GRAY);
|
692 |
//draw polygons
|
693 |
g.fillPolygon(poly1); |
694 |
g.fillPolygon(poly2); |
695 |
g.fillPolygon(poly3); |
696 |
g.fillPolygon(poly4); |
697 |
} |
698 |
|
699 |
} |
700 |
|
701 |
/*
|
702 |
* Simulator thread.
|
703 |
*
|
704 |
*/
|
705 |
private class Simulator extends Thread { |
706 |
final int SIMULATOR_DELAY = 300; |
707 |
boolean running;
|
708 |
|
709 |
public Simulator () {
|
710 |
super("Simulator"); |
711 |
running = false;
|
712 |
} |
713 |
|
714 |
public void run () { |
715 |
running = true;
|
716 |
while (running) {
|
717 |
step(); |
718 |
try {
|
719 |
Thread.sleep(SIMULATOR_DELAY);
|
720 |
} catch (InterruptedException e) { |
721 |
running = false;
|
722 |
return;
|
723 |
} |
724 |
} |
725 |
} |
726 |
|
727 |
private void step () { |
728 |
// simulate passing the token
|
729 |
moveToken(); |
730 |
} |
731 |
|
732 |
} |
733 |
|
734 |
/*
|
735 |
* PacketMonitor thread.
|
736 |
*
|
737 |
* Currently, this counts the rate of token passes but will eventually
|
738 |
* be modified to keep more important statistics.
|
739 |
*/
|
740 |
private class PacketMonitor extends Thread { |
741 |
final int PACKETMONITOR_DELAY = 1000; |
742 |
|
743 |
boolean running;
|
744 |
int tokenPasses;
|
745 |
|
746 |
public PacketMonitor () {
|
747 |
super("PacketMonitor"); |
748 |
running = false;
|
749 |
tokenPasses = 0;
|
750 |
} |
751 |
|
752 |
public void run () { |
753 |
running = true;
|
754 |
while (running) {
|
755 |
displayTokenPasses(); |
756 |
try {
|
757 |
Thread.sleep(PACKETMONITOR_DELAY);
|
758 |
} catch (InterruptedException e) { |
759 |
running = false;
|
760 |
return;
|
761 |
} |
762 |
} |
763 |
} |
764 |
|
765 |
public synchronized void addTokenPass () { |
766 |
tokenPasses++; |
767 |
} |
768 |
|
769 |
public synchronized void displayTokenPasses () { |
770 |
lblTokenPasses.setText("" + tokenPasses);
|
771 |
tokenPasses = 0;
|
772 |
} |
773 |
|
774 |
} |
775 |
|
776 |
/*
|
777 |
* DataUpdater thread.
|
778 |
*
|
779 |
*/
|
780 |
class DataUpdater extends Thread { |
781 |
final int DATAUPDATER_DELAY = 666; |
782 |
|
783 |
public DataUpdater () {
|
784 |
super("Colonet DataUpdater"); |
785 |
} |
786 |
|
787 |
public void run () { |
788 |
String line;
|
789 |
while (true) { |
790 |
try {
|
791 |
if (csi == null) return; |
792 |
line = csi.getLine(); |
793 |
if (line != null) { |
794 |
log.append("Incoming data: [" + line + "]"); |
795 |
//TODO: parse incoming data here
|
796 |
|
797 |
//request more data
|
798 |
//csi.sendRequest("Get all data", "server");
|
799 |
|
800 |
// update robot list
|
801 |
int numRobots = 5; |
802 |
String [] robotList = new String[numRobots+1]; |
803 |
robotList[0] = "All"; |
804 |
for (int i = 1; i <= numRobots; i++) |
805 |
robotList[i] = "Robot " + i;
|
806 |
cmbRobotNum = new JComboBox(robotList); |
807 |
} |
808 |
Thread.sleep(DATAUPDATER_DELAY);
|
809 |
} catch (InterruptedException e) { |
810 |
return;
|
811 |
} |
812 |
} |
813 |
} |
814 |
|
815 |
} |
816 |
|
817 |
} |