Revision 79
tried to merge lib_additions again...
Colonet.java | ||
---|---|---|
23 | 23 |
JButton btnConnect; |
24 | 24 |
JButton btnGraph; |
25 | 25 |
JLabel lblConnectionStatus; |
26 |
JTextArea txtInput;
|
|
26 |
JTextArea txtMatrix;
|
|
27 | 27 |
JTextArea txtInfo; |
28 | 28 |
JPanel panelConnect; |
29 | 29 |
JPanel panelServerInterface; |
... | ... | |
46 | 46 |
JPanel panelRobotDirection; |
47 | 47 |
JPanel panelRobotCommands; |
48 | 48 |
JButton btnF, btnB, btnL, btnR, btnActivate; |
49 |
JComboBox cmbRobotNum; |
|
49 | 50 |
|
50 | 51 |
// Task Manager |
51 | 52 |
JPanel panelTaskManager; |
... | ... | |
68 | 69 |
|
69 | 70 |
Socket socket; |
70 | 71 |
OutputStreamWriter out; //TODO: add a BufferedWriter |
71 |
DataListener datalistener;
|
|
72 |
DataUpdater dataUpdater;
|
|
72 | 73 |
|
73 | 74 |
Font botFont; |
74 | 75 |
Random random = new Random(); |
... | ... | |
78 | 79 |
volatile Rectangle[] botRect; //contains boundary shapes around bots for click detection |
79 | 80 |
|
80 | 81 |
Thread drawThread; |
82 |
Simulator simulator; |
|
81 | 83 |
SelectionIndicator indicator; |
82 | 84 |
PacketMonitor packetMonitor; |
83 | 85 |
ColonetServerInterface csi; |
... | ... | |
121 | 123 |
panel = new JPanel(false); //set automatic double-buffering to false. we are doing it manually. |
122 | 124 |
|
123 | 125 |
// Connection area |
124 |
txtInput = new JTextArea("- 9 3 - 1\n- - - 5 -\n4 - - - 2\n- - - - -\n1 - - 3 -");
|
|
125 |
txtInput.setBorder(BorderFactory.createTitledBorder("Input Matrix"));
|
|
126 |
txtMatrix = new JTextArea("- 9 3 - 1\n- - - 5 -\n4 - - - 2\n- - - - -\n1 - - 3 -");
|
|
127 |
txtMatrix.setBorder(BorderFactory.createTitledBorder("Input Matrix"));
|
|
126 | 128 |
txtInfo = new JTextArea(); |
127 | 129 |
txtInfo.setBorder(BorderFactory.createTitledBorder("Info")); |
128 | 130 |
txtInfo.setEditable(false); |
... | ... | |
144 | 146 |
panelServerInterface = new JPanel(); |
145 | 147 |
panelServerInterface.setLayout(new GridLayout(2,1)); |
146 | 148 |
panelServerInterface.add(panelConnect); |
147 |
panelServerInterface.add(txtInput);
|
|
149 |
panelServerInterface.add(txtMatrix);
|
|
148 | 150 |
|
149 | 151 |
// Status Elements |
150 | 152 |
lblTokenPasses = new JLabel(); |
... | ... | |
158 | 160 |
panelStats.add(new JLabel("Token Passes / sec ")); |
159 | 161 |
panelStats.add(lblTokenPasses); |
160 | 162 |
|
161 |
//TODO: add panelStats somewhere!
|
|
163 |
//TODO: add panelStats somewhere?
|
|
162 | 164 |
|
163 | 165 |
// Robot direction panel |
164 | 166 |
panelRobotDirection = new JPanel(); |
... | ... | |
182 | 184 |
// Robot Control and Commands |
183 | 185 |
panelRobotCommands = new JPanel(); |
184 | 186 |
panelRobotCommands.setLayout(new FlowLayout()); |
187 |
cmbRobotNum = new JComboBox(); |
|
188 |
panelRobotCommands.add(cmbRobotNum); |
|
185 | 189 |
panelRobotCommands.add(new JLabel("Commands go here")); |
186 | 190 |
panelRobotControl = new JPanel(); |
187 | 191 |
panelRobotControl.setLayout(new GridLayout(2,1)); |
... | ... | |
248 | 252 |
this.getContentPane().add(panelControl, BorderLayout.EAST); |
249 | 253 |
this.setVisible(true); |
250 | 254 |
|
255 |
/* Add all listeners here */ |
|
256 |
// Task Management |
|
251 | 257 |
btnAddTask.addActionListener(this); |
252 | 258 |
btnRemoveTask.addActionListener(this); |
253 | 259 |
btnMoveTaskUp.addActionListener(this); |
254 | 260 |
btnMoveTaskDown.addActionListener(this); |
261 |
// Robot Control |
|
262 |
btnF.addActionListener(this); |
|
263 |
btnB.addActionListener(this); |
|
264 |
btnL.addActionListener(this); |
|
265 |
btnR.addActionListener(this); |
|
266 |
btnActivate.addActionListener(this); |
|
267 |
// Other |
|
255 | 268 |
btnGraph.addActionListener(this); |
256 | 269 |
btnConnect.addActionListener(this); |
257 | 270 |
panel.addMouseListener(this); |
... | ... | |
277 | 290 |
indicator = new SelectionIndicator(canvas); |
278 | 291 |
indicator.setRadius(RADIUS+3, 15); //a tad more than the bot radius |
279 | 292 |
packetMonitor = new PacketMonitor(); |
280 |
datalistener = new DataListener();
|
|
293 |
simulator = new Simulator();
|
|
281 | 294 |
|
282 |
csi = new ColonetServerInterface(log); |
|
295 |
csi = new ColonetServerInterface(log, txtMatrix);
|
|
283 | 296 |
|
284 | 297 |
} |
285 | 298 |
|
... | ... | |
310 | 323 |
drawThread.start(); |
311 | 324 |
indicator.start(); |
312 | 325 |
packetMonitor.start(); |
326 |
simulator.start(); |
|
313 | 327 |
} else if (source == btnConnect) { |
314 | 328 |
doSocket(); |
329 |
dataUpdater = new DataUpdater(); |
|
330 |
dataUpdater.start(); |
|
315 | 331 |
} else if (source == btnRemoveTask) { |
316 | 332 |
try { |
317 | 333 |
taskListModel.remove(taskList.getSelectedIndex()); |
318 | 334 |
} catch (ArrayIndexOutOfBoundsException ex) { |
319 | 335 |
} |
336 |
|
|
337 |
// Robot controls |
|
338 |
} else if (source == btnF) { |
|
339 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " 0 200", ColonetServerInterface.GLOBAL_DEST); |
|
340 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " 0 200", ColonetServerInterface.GLOBAL_DEST); |
|
341 |
} else if (source == btnB) { |
|
342 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " 1 200", ColonetServerInterface.GLOBAL_DEST); |
|
343 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " 1 200", ColonetServerInterface.GLOBAL_DEST); |
|
344 |
} else if (source == btnL) { |
|
345 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " 1 200", ColonetServerInterface.GLOBAL_DEST); |
|
346 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " 0 200", ColonetServerInterface.GLOBAL_DEST); |
|
347 |
} else if (source == btnR) { |
|
348 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " 0 200", ColonetServerInterface.GLOBAL_DEST); |
|
349 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " 1 200", ColonetServerInterface.GLOBAL_DEST); |
|
350 |
} else if (source == btnActivate) { |
|
351 |
csi.sendData(ColonetServerInterface.MOTOR1_SET + " 0 0", ColonetServerInterface.GLOBAL_DEST); |
|
352 |
csi.sendData(ColonetServerInterface.MOTOR2_SET + " 0 0", ColonetServerInterface.GLOBAL_DEST); |
|
320 | 353 |
} |
321 | 354 |
} |
322 | 355 |
|
... | ... | |
337 | 370 |
if (i != count-1) s.append("\n"); |
338 | 371 |
} |
339 | 372 |
|
340 |
txtInput.setText(s.toString());
|
|
373 |
txtMatrix.setText(s.toString());
|
|
341 | 374 |
} |
342 | 375 |
|
343 | 376 |
private void doSocket () { |
... | ... | |
422 | 455 |
canvas.fillRect(0, 0, image.getWidth(), image.getHeight()); |
423 | 456 |
|
424 | 457 |
// parse the matrix, to see what robots exist |
425 |
String [] rows = txtInput.getText().split("\n");
|
|
458 |
String [] rows = txtMatrix.getText().split("\n");
|
|
426 | 459 |
numBots = rows.length; |
427 | 460 |
String [][] entries = new String[numBots][numBots]; |
428 | 461 |
valid = true; |
... | ... | |
432 | 465 |
} |
433 | 466 |
|
434 | 467 |
if (valid) { |
435 |
this.showStatus("Matrix OK");
|
|
468 |
this.showStatus("Running");
|
|
436 | 469 |
|
437 | 470 |
// draw robots and find which one is seleced |
438 | 471 |
double angle = 2.0 * Math.PI / numBots; |
... | ... | |
463 | 496 |
if (!entries[row][col].equals("-") && entries[col][row].equals("-") && row != col) { |
464 | 497 |
//TODO: Make a standard gray |
465 | 498 |
drawConnection(row, col, BIGRADIUS-RADIUS, new Color(200,200,200)); |
466 |
} |
|
467 |
else if (!entries[row][col].equals("-") && ! entries[col][row].equals("-") && row != col) { |
|
499 |
} else if (!entries[row][col].equals("-") && ! entries[col][row].equals("-") && row != col) { |
|
468 | 500 |
drawConnection(row, col, BIGRADIUS-RADIUS, Color.BLACK); |
469 | 501 |
} |
470 | 502 |
} |
... | ... | |
472 | 504 |
|
473 | 505 |
// draw the selection indicator |
474 | 506 |
indicator.draw(); |
475 |
txtInfo.setText("Packet statistics: ???"); |
|
476 | 507 |
|
477 | 508 |
} else {// if matrix is not valid |
478 | 509 |
this.showStatus("Error: Invalid matrix"); |
... | ... | |
604 | 635 |
|
605 | 636 |
//recalculate radius, if it will look cool, lolz |
606 | 637 |
int newr = r; |
607 |
if (steps < 100) newr = (int)( r + 200/(steps+1) ); |
|
638 |
if (steps < 100) |
|
639 |
newr = (int)( r + 200/(steps+1) ); |
|
608 | 640 |
|
609 | 641 |
//precompute values for dx and dy |
610 | 642 |
int dx_inner = (int)(newr * Math.cos(theta)); |
... | ... | |
746 | 778 |
} |
747 | 779 |
|
748 | 780 |
/* |
749 |
* DataListener thread. |
|
781 |
* DataUpdater thread. |
|
782 |
* The purpose of this thread is to request data from the server at regular intervals. |
|
750 | 783 |
* |
751 | 784 |
*/ |
752 |
class DataListener extends Thread { |
|
753 |
final int DATALISTENER_DELAY = 1000; |
|
754 |
BufferedReader reader; |
|
785 |
class DataUpdater extends Thread { |
|
786 |
final int DATAUPDATER_DELAY = 1100; |
|
755 | 787 |
|
756 |
public DataListener () {
|
|
757 |
super("Colonet DataListener");
|
|
788 |
public DataUpdater () {
|
|
789 |
super("Colonet DataUpdater");
|
|
758 | 790 |
} |
759 | 791 |
|
760 | 792 |
public void run () { |
761 | 793 |
String line; |
762 |
reader = csi.getBufferedReader(); |
|
763 | 794 |
while (true) { |
764 | 795 |
try { |
765 |
if (csi.isReady()) { |
|
766 |
line = reader.readLine(); |
|
767 |
if (line != null) { |
|
768 |
csi.msg("Incoming data: [" + line + "]"); |
|
769 |
//TODO: parse incoming data here |
|
770 |
} |
|
771 |
} |
|
772 |
Thread.sleep(DATALISTENER_DELAY); |
|
796 |
//request more data |
|
797 |
if (csi.isReady()) |
|
798 |
csi.sendRequest(ColonetServerInterface.REQUEST_BOM_MATRIX, ""); |
|
799 |
Thread.sleep(DATAUPDATER_DELAY); |
|
773 | 800 |
} catch (InterruptedException e) { |
774 | 801 |
return; |
775 |
} catch (IOException e) { |
|
776 |
csi.warn("IOException while reading incoming data."); |
|
777 |
} |
|
802 |
} |
|
778 | 803 |
} |
779 | 804 |
} |
780 | 805 |
|
Also available in: Unified diff