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