Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / colonet / client / Colonet.java @ 710

History | View | Annotate | Download (37.5 KB)

1
import javax.swing.*;
2
import javax.swing.event.*;
3
import javax.imageio.*;
4
import java.awt.*;
5
import java.awt.image.*;
6
import java.awt.event.*;
7
import java.net.*;
8
import java.io.*;
9
import java.util.*;
10

    
11
/**
12
* The Colonet Graphical User Interface Applet for use locally and over an internet connection.
13
* @author Gregory Tress
14
*
15
* To generate javadoc on this file or other java files, use javadoc *.java -d doc, where doc
16
* is the name of the folder into which the files should be written.
17
*/
18
public class Colonet extends JApplet implements ActionListener, MouseInputListener, KeyListener {
19

    
20
        // Connection
21
        JTextField txtHost;
22
        JTextField txtPort;
23
        JButton btnConnect;
24
        JButton btnGetXBeeIDs;
25
        JLabel lblConnectionStatus;
26
        JTextArea txtInfo;
27
        JPanel panelConnect;
28
        JPanel panelServerInterface;
29
        Socket socket;
30
        DataUpdater dataUpdater;
31

    
32
        // Control
33
        JTabbedPane tabPaneControl;
34
        JPanel panelRobotControl;
35
        JPanel panelRobotDirection;
36
        JPanel panelRobotDirectionButtons;
37
        JPanel panelRobotCommands;
38
        JButton btnF, btnB, btnL, btnR, btnActivate;
39
        JLabel lblBattery;
40
        JLabel lblSelected;
41
        BatteryIcon batteryIcon;
42
        JPanel panelBattery;
43
        VectorController vectorController;
44
        BufferedImage imageVectorControl;
45
        JButton btnAssignID;
46
        JButton btnLocateStation;
47
        boolean setStation;
48
        ChargingStation station;
49
        boolean setWaypoint;
50
        int setWaypointID;
51
  JButton btnSetBounds;
52
  JButton btnClearBounds;
53
  RobotBoundary boundary;
54
        JButton btnCommand_MoveTo;
55
        JButton btnCommand_MoveAll;
56
        JButton btnCommand_StopTask;
57
        JButton btnCommand_ResumeTask;
58
        JButton btnCommand_ChargeNow;
59
        JButton btnCommand_StopCharging;
60

    
61
        // Task Manager
62
        JPanel panelTaskManager;
63
        JScrollPane spTaskManager;
64
        JPanel panelTaskManagerControls;
65
        JPanel panelTaskManagerControlsPriority;
66
        DefaultListModel taskListModel;
67
        JList taskList;
68
        JButton btnAddTask;
69
        JButton btnRemoveTask;
70
        JButton btnMoveTaskUp;
71
        JButton btnMoveTaskDown;
72
        JButton btnUpdateTasks;
73
        TaskAddWindow taskAddWindow;
74

    
75
        // Webcam
76
        WebcamPanel panelWebcam;
77
        GraphicsConfiguration gc;
78
        JTabbedPane tabPaneMain;
79
        WebcamLoader webcamLoader;
80
        
81
        // Robots
82
        volatile int selectedBot;         //the user has selected this bot graphically
83
        volatile RobotList robotIcons;         //contains boundary shapes around bots for click detection
84
        volatile int[] xbeeID;
85

    
86
        Colonet self = this;
87
        volatile ColonetServerInterface csi;
88

    
89
        public void init () {
90
                // Set the default look and feel
91
                String laf = ColonetConstants.LOOK_AND_FEEL;
92
                try {
93
                        UIManager.setLookAndFeel(laf);
94
                } catch (UnsupportedLookAndFeelException exc) {
95
                        System.err.println ("Warning: UnsupportedLookAndFeel: " + laf);
96
                } catch (Exception exc) {
97
                        System.err.println ("Error loading " + laf + ": " + exc);
98
                }
99

    
100
                // We should invoke and wait to avoid browser display difficulties
101
                Runnable r = new Runnable() {
102
                        public void run() {
103
                                createAndShowGUI();
104
                        }
105
                };
106

    
107
                try {
108
                        SwingUtilities.invokeAndWait(r);
109
                } catch (InterruptedException e) {
110
                        //Not really sure why we would be in this situation
111
                        System.out.println("InterruptedException in init: " + e);
112
                } catch (java.lang.reflect.InvocationTargetException e) {
113
                        //This could happen for various reasons if there is a problem in createAndShowGUI
114
                        e.printStackTrace();
115
                }
116
        }
117

    
118
        public void destroy () {
119
                if (csi != null) {
120
                        csi.disconnect();
121
                }
122
        }
123

    
124
        private synchronized void createAndShowGUI () {
125
                // Webcam
126
                gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
127
                panelWebcam = new WebcamPanel();
128
                tabPaneMain = new JTabbedPane();
129
                tabPaneMain.setFont(new Font("arial", Font.PLAIN, 16));
130
                tabPaneMain.add(panelWebcam, "Webcam");
131

    
132
                // Robots
133
                selectedBot = -1;
134
                robotIcons = new RobotList();
135

    
136
                // Connection area
137
                txtInfo = new JTextArea();
138
                txtInfo.setBorder(BorderFactory.createTitledBorder("Info"));
139
                txtHost = new JTextField(this.getDocumentBase().getHost());
140
                txtHost.setBorder(BorderFactory.createTitledBorder("Host"));
141
                txtPort = new JTextField("10123");
142
                txtPort.setBorder(BorderFactory.createTitledBorder("Port"));
143
                btnConnect = new JButton("Connect");
144
                btnConnect.setFont(new Font("arial", Font.BOLD, 16));
145
                btnGetXBeeIDs = new JButton("Get XBee IDs");
146
                getRootPane().setDefaultButton(btnConnect);
147
                lblConnectionStatus = new JLabel("Status: Offline");
148
                panelConnect = new JPanel();
149
                panelConnect.setLayout(new GridLayout(6,1));
150
                panelConnect.add(lblConnectionStatus);
151
                panelConnect.add(txtHost);
152
                panelConnect.add(txtPort);
153
                panelConnect.add(btnConnect);
154
                panelServerInterface = new JPanel();
155
                panelServerInterface.setLayout(new GridLayout(2,1));
156
                panelServerInterface.add(panelConnect);
157
                panelServerInterface.add(txtInfo);
158

    
159
                // Robot direction panel
160
                panelRobotDirection = new JPanel();
161
                panelRobotDirectionButtons = new JPanel();
162

    
163
                Font f = new Font(null, Font.PLAIN, 18);
164
                btnF = new JButton("\u2191");
165
                btnF.setFont(f);
166
                btnB = new JButton("\u2193");
167
                btnB.setFont(f);
168
                btnL = new JButton("\u2190");
169
                btnL.setFont(f);
170
                btnR = new JButton("\u2192");
171
                btnR.setFont(f);
172
                btnActivate = new JButton("\u25A0");
173
                btnActivate.setFont(new Font(null, Font.BOLD, 24));
174

    
175
                panelRobotDirectionButtons.setLayout(new GridLayout(1,5));
176
                panelRobotDirectionButtons.add(btnActivate);
177
                panelRobotDirectionButtons.add(btnF);
178
                panelRobotDirectionButtons.add(btnB);
179
                panelRobotDirectionButtons.add(btnL);
180
                panelRobotDirectionButtons.add(btnR);
181

    
182
                imageVectorControl = gc.createCompatibleImage(ColonetConstants.VECTOR_CONTROLLER_WIDTH, ColonetConstants.VECTOR_CONTROLLER_HEIGHT);
183
                vectorController = new VectorController(imageVectorControl, self);
184
                panelRobotDirection.setLayout(new BorderLayout());
185
                panelRobotDirection.add(vectorController, BorderLayout.CENTER);
186
                panelRobotDirection.add(panelRobotDirectionButtons, BorderLayout.SOUTH);
187

    
188
                // Robot Control and Commands
189
                panelRobotCommands = new JPanel();
190
                panelRobotCommands.setLayout(new GridLayout(5,2));
191
                // Battery subset
192
                batteryIcon = new BatteryIcon(0);
193
                lblBattery = new JLabel(batteryIcon);
194
                lblSelected = new JLabel("None");
195
                // Management subset
196
                setStation = false;
197
                setWaypoint = false;
198
                setWaypointID = -1;
199
                btnAssignID = new JButton("Assign ID");
200
                btnLocateStation = new JButton("Locate Charge Station");
201
    boundary = new RobotBoundary();
202
    btnSetBounds = new JButton("Set Boundary");
203
    btnClearBounds = new JButton("Clear Boundary");
204
    // Control subset
205
                btnCommand_MoveTo = new JButton("Move to ...");
206
                btnCommand_MoveAll = new JButton("Move all ...");
207
                btnCommand_StopTask = new JButton("Stop Current Task");
208
                btnCommand_ResumeTask = new JButton("Resume Current Task");
209
                btnCommand_ChargeNow = new JButton("Recharge Now");
210
                btnCommand_StopCharging = new JButton("Stop Recharging");
211
                panelRobotCommands.add(new JLabel("Battery Level: "));
212
                panelRobotCommands.add(lblBattery);
213
                panelRobotCommands.add(new JLabel("Selected Icon: "));
214
                panelRobotCommands.add(lblSelected);
215
                panelRobotCommands.add(btnAssignID);
216
    panelRobotCommands.add(btnLocateStation);
217
                panelRobotCommands.add(btnSetBounds);
218
    panelRobotCommands.add(btnClearBounds);
219
                panelRobotCommands.add(btnCommand_MoveTo);
220
                panelRobotCommands.add(btnCommand_MoveAll);
221
                //panelRobotCommands.add(btnCommand_StopTask);
222
                //panelRobotCommands.add(btnCommand_ResumeTask);
223
                //panelRobotCommands.add(btnCommand_ChargeNow);
224
                //panelRobotCommands.add(btnCommand_StopCharging);
225
                panelRobotControl = new JPanel();
226
                panelRobotControl.setLayout(new GridLayout(2,1));
227
                panelRobotControl.add(panelRobotDirection);
228
                panelRobotControl.add(panelRobotCommands);
229

    
230
                // Task Manager
231
                panelTaskManager = new JPanel();
232
                panelTaskManager.setLayout(new BorderLayout());
233
                taskListModel = new DefaultListModel();
234
                taskList = new JList(taskListModel);
235
                taskList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
236
                taskList.setSelectedIndex(0);
237
                spTaskManager = new JScrollPane(taskList);
238
                panelTaskManagerControls = new JPanel();
239
                panelTaskManagerControls.setLayout(new GridLayout(1,4));
240
                panelTaskManagerControlsPriority = new JPanel();
241
                panelTaskManagerControlsPriority.setLayout(new GridLayout(1,2));
242
                btnAddTask = new JButton("Add...");
243
                btnRemoveTask = new JButton("Remove");
244
                btnMoveTaskUp = new JButton("^");
245
                btnMoveTaskDown = new JButton("v");
246
                btnUpdateTasks = new JButton("Update");
247
                panelTaskManagerControlsPriority.add(btnMoveTaskUp);
248
                panelTaskManagerControlsPriority.add(btnMoveTaskDown);
249
                panelTaskManagerControls.add(btnAddTask);
250
                panelTaskManagerControls.add(btnRemoveTask);
251
                panelTaskManagerControls.add(btnUpdateTasks);
252
                panelTaskManagerControls.add(panelTaskManagerControlsPriority);
253
                panelTaskManager.add(spTaskManager, BorderLayout.CENTER);
254
                panelTaskManager.add(panelTaskManagerControls, BorderLayout.SOUTH);
255
                panelTaskManager.add(new JLabel("Current Task Queue"), BorderLayout.NORTH);
256
                taskAddWindow = new TaskAddWindow();
257

    
258
                // Main control mechanism
259
                tabPaneControl = new JTabbedPane(JTabbedPane.TOP);
260
                tabPaneControl.setFont(new Font("arial", Font.PLAIN, 16));
261
                tabPaneControl.setPreferredSize(new Dimension(ColonetConstants.VECTOR_CONTROLLER_WIDTH, 0));
262
                tabPaneControl.addTab("Connection", panelServerInterface);
263
                tabPaneControl.addTab("Robots", panelRobotControl);
264
                //tabPaneControl.addTab("Tasks", panelTaskManager);
265

    
266
                // Put all elements in the ContentPane
267
                this.getContentPane().setLayout(new BorderLayout());
268
                this.getContentPane().add(tabPaneMain, BorderLayout.CENTER);
269
                this.getContentPane().add(tabPaneControl, BorderLayout.EAST);
270
                this.setVisible(true);
271
                
272
                // Disable components before connecting
273
                btnConnect.setText("Connect");
274
    lblConnectionStatus.setText("Status: Disconnected");
275
    setComponentsEnabled(false);
276

    
277
                /* Add all listeners here */
278
                // Task Management
279
                btnAddTask.addActionListener(this);
280
                btnRemoveTask.addActionListener(this);
281
                btnMoveTaskUp.addActionListener(this);
282
                btnMoveTaskDown.addActionListener(this);
283
                btnUpdateTasks.addActionListener(this);
284
                // Robot Control
285
                btnF.addActionListener(this);
286
                btnB.addActionListener(this);
287
                btnL.addActionListener(this);
288
                btnR.addActionListener(this);
289
                btnF.addKeyListener(this);
290
                btnB.addKeyListener(this);
291
                btnL.addKeyListener(this);
292
                btnR.addKeyListener(this);
293
                btnActivate.addActionListener(this);
294
                btnActivate.addKeyListener(this);
295
                btnCommand_MoveTo.addActionListener(this);
296
                btnCommand_MoveAll.addActionListener(this);
297
                btnCommand_StopTask.addActionListener(this);
298
                btnCommand_ResumeTask.addActionListener(this);
299
                btnCommand_ChargeNow.addActionListener(this);
300
                btnCommand_StopCharging.addActionListener(this);
301
                // Other
302
                btnConnect.addActionListener(this);
303
                btnGetXBeeIDs.addActionListener(this);
304
                btnAssignID.addActionListener(this);
305
                btnLocateStation.addActionListener(this);
306
    btnSetBounds.addActionListener(this);
307
    btnClearBounds.addActionListener(this);
308
                panelWebcam.addMouseListener(this);
309
                panelWebcam.addMouseMotionListener(this);
310
        }
311
        
312
        public ColonetServerInterface getCSI () {
313
            return csi;
314
        }
315

    
316
        public void paint (Graphics g) {
317
                super.paint(g);
318
        }
319

    
320
        public void update (Graphics g) {
321
                paint(g);
322
        }
323

    
324
        /**
325
        * Gets the JTextArea used for displaying debugging information. 
326
        *
327
        * @return the JTextArea where debugging information can be displayed.
328
        */
329
        public JTextArea getInfoPanel () {
330
                return txtInfo;
331
        }
332

    
333
        /**
334
        * Parses a String containing BOM matrix information.
335
        * The ColonetServerInterface receives lines of the BOM matrix.        (For encoding
336
        * information, see the ColonetServerInterface documentation.)         The entire matrix is passed
337
        * to the client when requested. This method takes a string of the form
338
        * "[command code] [command code] [number of robots] [data0] [data1] ..."
339
        * with tokens separated by spaces and containing no brackets.
340
        * The [command code]s are predefined values identifying this String as a BOM data
341
        * String, [number of robots] is an integer, and the values that follow are
342
        * the sensor readings of the robots in order, starting with robot 0.        Only [number of robots]^2
343
        * data entries will be read.        The matrix values are saved locally until the next String is parsed.
344
        *
345
        *
346
        * @param line the String containing BOM matrix information.
347
        * @throws ArrayIndexOutOfBoundsException if there are fewer than [number of robots]^2 data entries in the String
348
        */
349
        public void parseMatrix (String line) {
350
                txtInfo.setText("");
351
                String [] str = line.split(" ");
352
                int num = Integer.parseInt(str[2]);
353
                for (int i = 0; i < num; i++) {
354
                        for (int j = 0; j < num; j++) {
355
                                String next = str[3 + i*num + j];
356
                                if (next.equals("-1")) {
357
                                        txtInfo.append("-");
358
                                } else {
359
                                        txtInfo.append(next);
360
                                }
361

    
362
                                if (j < num - 1) {
363
                                        txtInfo.append(" ");
364
                                }
365
                        }
366

    
367
                        if (i < num - 1) {
368
                                txtInfo.append("\n");
369
                        }
370
                }
371
                repaint();
372
        }
373

    
374
        public void connect () {
375
    if (csi != null)
376
      return;
377
    csi = new ColonetServerInterface(self);
378
    csi.connect(txtHost.getText(), txtPort.getText());
379
    if (!csi.isReady()) {
380
      csi = null;
381
      return;
382
    }
383
    webcamLoader = new WebcamLoader(self);
384
    dataUpdater = new DataUpdater();
385
    dataUpdater.start();
386
    webcamLoader.start();
387
    Runnable r = new Runnable() {
388
      public void run () {
389
        btnConnect.setText("Disconnect");
390
        lblConnectionStatus.setText("Status: Connected");
391
        setComponentsEnabled(true);
392
      }
393
    };
394
    SwingUtilities.invokeLater(r);
395
        }
396

    
397
        public void disconnect () {
398
    try {
399
        dataUpdater.interrupt();
400
    } catch (Exception e) {
401
    }
402
    csi = null;
403
                Runnable r = new Runnable() {
404
      public void run () {
405
                    btnConnect.setText("Connect");
406
              lblConnectionStatus.setText("Status: Disconnected");
407
        setComponentsEnabled(false);
408
        boundary = null;
409
        station = null;
410
            }
411
                };
412
                SwingUtilities.invokeLater(r);
413
        }
414
        
415
        private void setComponentsEnabled (boolean enabled) {
416
    btnF.setEnabled(enabled);
417
    btnB.setEnabled(enabled);
418
    btnL.setEnabled(enabled);
419
    btnR.setEnabled(enabled);
420
    btnActivate.setEnabled(enabled);
421
    btnAssignID.setEnabled(enabled);
422
    btnLocateStation.setEnabled(enabled);
423
    btnSetBounds.setEnabled(enabled);
424
    btnClearBounds.setEnabled(enabled);
425
    btnCommand_MoveTo.setEnabled(enabled);
426
    btnCommand_MoveAll.setEnabled(enabled);
427
        }
428

    
429
        /**
430
        * Parses a String containing a task queue update.
431
        * Format is currently not specified.
432
        * This method currently does nothing.
433
        *
434
        * @param line the String containing task queue update information.
435
        */
436
        public void parseQueue (String line) {
437

    
438
        }
439

    
440
        /**
441
        * Parses a String containing XBee ID values.
442
        * The ColonetServerInterface receives Strings of XBee information.        (For encoding
443
        * information, see the ColonetServerInterface documentation.)         This method takes
444
        * a string of the form "[command code] [command code] [number of robots] [id0] [id1] ..."
445
        * with tokens separated by spaces and containing no brackets.
446
        * The [command code]s are predefined values identifying this String as an XBee
447
        * ID String, [number of robots] is an integer, and the values that follow are
448
        * the IDs of the robots in order, starting with robot 0.        Only [number of robots]
449
        * will be read.         The ID values are saved locally until the next String is parsed.
450
        * The purpose of having this list is to ensure that robots are properly identified for control purposes.
451
        * This keeps robot identification consistent between sessions and prevents arbitrary assignment.
452
        *
453
        * @param line the String containing XBee ID information.
454
        * @throws ArrayIndexOutOfBoundsException if there are fewer than [number of robots] IDs in the String
455
        * @see ColonetServerInterface#sendXBeeIDRequest()
456
        */
457
        public void parseXBeeIDs (String line) {
458
                String [] str = line.split(" ");
459
                int num = Integer.parseInt(str[2]);
460
                xbeeID = new int[num];
461
                for (int i = 0; i < num; i++) {
462
                        xbeeID[i] = Integer.parseInt(str[i+3]);
463
                }
464
        }
465

    
466
        /**
467
        * Parses a String containing battery information.
468
        * The ColonetServerInterface receives Strings of battery information.         (For encoding
469
        * information, see the ColonetServerInterface documentation.)         This method takes
470
        * a string of the form "[command code] [command code] [robot ID] [value]"
471
        * with tokens separated by spaces and containing no brackets.
472
        * The [command code]s are predefined values identifying this String as a battery
473
        * information String, [robot ID] is an integer, and [value] is a battery measurement.
474
        * This updates the batery information for a single robot.
475
        *
476
        *
477
        * @param line the String containing battery information.
478
        * @see ColonetServerInterface#sendBatteryRequest(int)
479
        */
480
        public void parseBattery (String line) {
481
            int botNum, level;
482
            try {
483
                    String [] str = line.split(" ");
484
                    botNum = Integer.parseInt(str[2]);
485
                    level = Integer.parseInt(str[3]);
486
                } catch (NumberFormatException e) {
487
                    System.out.println("Could not parse battery update");
488
                    return;
489
                }
490

    
491
                RobotIcon r = robotIcons.get(botNum);
492
                if (r != null) {
493
                    r.battery = batteryIcon.convert(level);  // set contextual battery meter
494
                    batteryIcon.setLevel(level);             // set solo battery meter
495
                }
496
                super.repaint();
497
        }
498

    
499
        /**
500
        * Parses a String containing visual robot position information along with
501
        * canonical ID assignments.
502
        */
503
        public void parsePositions (String line) {
504
                String [] str = line.split(" ");
505
                RobotList newList = new RobotList();
506

    
507
                for (int i = 2; i < str.length; i+=3) {
508
                        int id = Integer.parseInt(str[i]);
509
                        int x = Integer.parseInt(str[i+1]);
510
                        int y = Integer.parseInt(str[i+2]);
511
                        RobotIcon newIcon = new RobotIcon(id, x, y);
512
                        // Save previous robot information
513
                        RobotIcon oldIcon = robotIcons.get(id);
514
                        if (oldIcon != null) {
515
                            newIcon.battery = oldIcon.battery;
516
                                newIcon.destx = oldIcon.destx;
517
                                newIcon.desty = oldIcon.desty;
518
                        }
519
                        if (newIcon.id >= 0) {
520
                                newIcon.color = Color.GREEN;
521
                        }
522
                        newList.put(id, newIcon);
523
                }
524
                robotIcons = newList;
525
                repaint();
526
        }
527
  
528
  /**
529
    * Parses a message that indicates a robot has reached its destination
530
   */
531
  public void parseMoveUpdate (String line) {
532
    System.out.println("Got move update: " + line);
533
    String [] str = line.split(" ");
534
    int id = Integer.parseInt(str[1]);
535
    robotIcons.cancelMoveTo(id);
536
  }
537
        
538
        /**
539
        * Set the ID of the selected robot
540
        */
541
        public void setSelectedBot (int id) {
542
            this.selectedBot = id;
543
        }
544
        
545
        /**
546
        * Returns the ID of the selected robot
547
        */
548
        public int getSelectedBot () {
549
            return this.selectedBot;
550
        }
551

    
552
        //
553
        // MouseListener methods
554
        //
555
        public void mousePressed(MouseEvent e) {
556
                //Start a new Thread to handle the MouseEvent
557
                (new MouseHandler(e)).start();
558
        }
559
        public void mouseExited(MouseEvent e) {
560
        }
561
        public void mouseEntered(MouseEvent e) {
562
        }
563
        public void mouseReleased(MouseEvent e) {
564
    (new MouseHandler(e)).start();
565
        }
566
        public void mouseClicked(MouseEvent e) {
567
        }
568
        public void mouseDragged(MouseEvent e) {
569
    (new MouseHandler(e)).start();
570
        }
571
        public void mouseMoved(MouseEvent e) {
572
        }
573

    
574
        //
575
        // KeyListener methods
576
        //
577
        public void keyPressed (KeyEvent e) {
578
                //Start a new Thread to handle the KeyEvent
579
                (new KeyHandler(e)).start();
580
                repaint();
581
        }
582
        public void keyReleased (KeyEvent e) {
583
        }
584
        public void keyTyped (KeyEvent e) {
585
        }
586

    
587
        //
588
        // ActionListener method
589
        //
590
        public void actionPerformed (ActionEvent e) {
591
                // Start a new Thread to handle the ActionEvent
592
                (new ActionHandler(e)).start();
593
                repaint();
594
        }
595

    
596
        class MouseHandler extends Thread {
597
                MouseEvent e;
598

    
599
                public MouseHandler (MouseEvent event) {
600
                        super("MouseHandler");
601
                        this.e = event;
602
                }
603

    
604
                public void run () {
605
                        Point pt = panelWebcam.convertClick(e);
606

    
607
                        // If we are selecting a waypoint (destination) for a specific bot
608
                        if (setWaypoint && setWaypointID >= 0 && e.getID() == MouseEvent.MOUSE_PRESSED) {
609
                          // If the user clicks outside the boundary, do nothing
610
                          if (boundary.isActive() && !boundary.contains(e.getX(), e.getY())) {
611
                            return;
612
                          }
613
                                setWaypoint = false;
614
                                panelWebcam.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
615
                                if (selectedBot < 0) {
616
                                        return;
617
                                }
618
                                
619
                                RobotIcon r = robotIcons.get(selectedBot);
620
                                if (r != null) {
621
                                        r.destx = pt.x;
622
                                        r.desty = pt.y;
623
                                        if (csi != null) {
624
                                                csi.sendAbsoluteMove(r.id, r.destx, r.desty);
625
                                        }
626
                                }
627
                                return;
628
                        }
629
                        
630
                        // If we are ordering a charge (right-click on a station)
631
                        if ((e.getButton() == MouseEvent.BUTTON2 || e.getButton() == MouseEvent.BUTTON3)  
632
          && e.getID() == MouseEvent.MOUSE_PRESSED
633
          && station != null && station.contains(e.getX(), e.getY())) {
634
        //TODO: send charge command
635
                                return;
636
                        }
637

    
638
                        // Right-click also means we are moving a robot
639
                        if ((e.getButton() == MouseEvent.BUTTON2 || e.getButton() == MouseEvent.BUTTON3)  
640
          && e.getID() == MouseEvent.MOUSE_PRESSED) {
641
        // If the user clicks outside the boundary, do nothing
642
                          if (boundary.isActive() && !boundary.contains(e.getX(), e.getY())) {
643
                            return;
644
                          }
645
                                if (selectedBot < 0) {
646
                                        return;
647
                                }
648

    
649
                                RobotIcon r = robotIcons.get(selectedBot);
650
                                if (r != null) {
651
                                        r.destx = pt.x;
652
                                        r.desty = pt.y;
653
                                        if (csi != null) {
654
                                                csi.sendAbsoluteMove(r.id, r.destx, r.desty);
655
                                        }
656
                                }
657
                                return;
658
                        }
659

    
660
                        // If we are setting all waypoints
661
                        if (setWaypoint && e.getID() == MouseEvent.MOUSE_PRESSED) {
662
                          // If the user clicks outside the boundary, do nothing
663
                          if (boundary.isActive() && !boundary.contains(e.getX(), e.getY())) {
664
                            return;
665
                          }
666
                                setWaypoint = false;
667
                                panelWebcam.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
668
                                for (Map.Entry<Integer,RobotIcon> entry : robotIcons.entrySet()) {
669
                                        RobotIcon r = entry.getValue();
670
                                        r.destx = pt.x;
671
                                        r.desty = pt.y;
672
                                }
673
                                return;
674
                        }
675
      
676
      // If we are drawing a boundary rectangle
677
      if (boundary != null && boundary.isSetting()) {
678
        // Begin 
679
        if (e.getID() == MouseEvent.MOUSE_PRESSED) {
680
          boundary.panel_p1 = new Point(e.getX(), e.getY());
681
          boundary.panel_p2 = new Point(e.getX(), e.getY());
682
          boundary.img_p1 = new Point(pt.x, pt.y);
683
        }
684
        // Resize
685
        else if (e.getID() == MouseEvent.MOUSE_DRAGGED){
686
          boundary.panel_p2 = new Point(e.getX(), e.getY());
687
        }
688
        // Finish
689
        else if (e.getID() == MouseEvent.MOUSE_RELEASED){
690
          boundary.panel_p2 = new Point(e.getX(), e.getY());
691
          boundary.img_p2 = new Point(pt.x, pt.y);
692
          panelWebcam.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
693
          boundary.set = false;
694
          boundary.active = true;
695
          boundary.sendToServer();
696
        }
697
        return;
698
      }
699
      
700
      // If we are locating a charging station
701
      if (setStation) {
702
        setStation = false;
703
        station = new ChargingStation(e.getX(), e.getY());
704
        station.setActive(true);
705
        return;
706
      }
707

    
708
                        // Otherwise, we are selecting a bot, or doing nothing
709
                        RobotIcon r = robotIcons.getBoundingIcon(pt);
710
      if (r != null)
711
        selectedBot = r.id;
712
                }
713
        }
714

    
715
        class KeyHandler extends Thread {
716
                KeyEvent e;
717

    
718
                public KeyHandler (KeyEvent event) {
719
                        super("KeyHandler");
720
                        this.e = event;
721
                }
722

    
723
                public void run () {
724
                        int code = e.getKeyCode();
725
                        if (code == KeyEvent.VK_UP) {
726
                                vectorController.setMaxForward();
727
                                vectorController.sendToServer();
728
                        } else if (code == KeyEvent.VK_DOWN) {
729
                                vectorController.setMaxReverse();
730
                                vectorController.sendToServer();
731
                        } else if (code == KeyEvent.VK_LEFT) {
732
                                vectorController.setMaxLeft();
733
                                vectorController.sendToServer();
734
                        } else if (code == KeyEvent.VK_RIGHT) {
735
                                vectorController.setMaxRight();
736
                                vectorController.sendToServer();
737
                        } else if (code == KeyEvent.VK_S) {
738
                                vectorController.setZero();
739
                                vectorController.sendToServer();
740
                        }
741
                }
742
        }
743

    
744
        class ActionHandler extends Thread {
745
                ActionEvent e;
746

    
747
                public ActionHandler (ActionEvent event) {
748
                        super("ActionHandler");
749
                        this.e = event;
750
                }
751

    
752
                public void run () {
753
                        Object source = e.getSource();
754

    
755
                        // General Actions
756
                        if (source == btnConnect) {
757
                                if (csi == null) {
758
                                        connect();
759
                                } else {
760
                                        disconnect();
761
                                }
762
                        } else if (source == btnGetXBeeIDs) {
763
                                csi.sendXBeeIDRequest();
764
                        } else if (source == btnAssignID) {
765
                                String message;
766
                                if (selectedBot < 0) {
767
                                        message = "That robot is unidentified. Please specify its ID.";
768
                                } else {
769
                                        message = "That robot has ID " + selectedBot + ". You may reassign it now.";
770
                                }
771
                                String result = JOptionPane.showInputDialog(self, message, "Robot Identification", JOptionPane.QUESTION_MESSAGE);
772
                                if (result == null) {
773
                                        return;
774
                                }
775
                                int newID = -1;
776
                                try {
777
                                        newID = Integer.parseInt(result);
778
                                } catch (Exception ex) {
779
                                        csi.warn("Invalid ID.");
780
                                        return;
781
                                }
782
                                // Assign new ID and update display
783
                                if (csi != null) {
784
                                        csi.sendIDAssignment(selectedBot, newID);
785
                                }
786
                                selectedBot = newID;
787
                                lblSelected.setText("" + newID);
788
                  } else if (source == btnLocateStation) {
789
                    setStation = true;
790
                        } else if (source == btnF) { // Robot Movement Controls
791
                                vectorController.setMaxForward();
792
                                vectorController.sendToServer();
793
                                robotIcons.cancelMoveTo(selectedBot);
794
                        } else if (source == btnB) {
795
                                vectorController.setMaxReverse();
796
                                vectorController.sendToServer();
797
                                robotIcons.cancelMoveTo(selectedBot);
798
                        } else if (source == btnL) {
799
                                vectorController.setMaxLeft();
800
                                vectorController.sendToServer();
801
                                robotIcons.cancelMoveTo(selectedBot);
802
                        } else if (source == btnR) {
803
                                vectorController.setMaxRight();
804
                                vectorController.sendToServer();
805
                                robotIcons.cancelMoveTo(selectedBot);
806
                        } else if (source == btnActivate) {
807
                                vectorController.setZero();
808
                                vectorController.sendToServer();
809
                                robotIcons.cancelMoveTo(selectedBot);
810
      } else if (source == btnSetBounds) {
811
        boundary.set = true;
812
        panelWebcam.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
813
      } else if (source == btnClearBounds) {
814
        boundary.active = false;
815
        boundary.set = false;
816
        boundary.img_p1 = new Point(-1, -1);
817
        boundary.img_p2 = new Point(-1, -1);
818
        boundary.panel_p1 = new Point(-1, -1);
819
        boundary.panel_p2 = new Point(-1, -1);
820
        boundary.sendToServer();
821
                        } else if (source == btnCommand_MoveTo) {
822
                                if (selectedBot < 0) {
823
                                        return;
824
                                }
825
                                panelWebcam.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
826
                                setWaypoint = true;
827
                                setWaypointID = selectedBot;
828
                        } else if (source == btnCommand_MoveAll) {
829
                                panelWebcam.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
830
                                setWaypoint = true;
831
                                setWaypointID = -1;
832
                        } else if (source == btnCommand_StopTask) {
833

    
834
                        } else if (source == btnCommand_ResumeTask) {
835

    
836
                        } else if (source == btnCommand_ChargeNow) {
837

    
838
                        } else if (source == btnCommand_StopCharging) {
839

    
840
                        } else if (source == btnAddTask) { // Queue Management
841
                                taskAddWindow.prompt();
842
                        } else if (source == btnRemoveTask) {
843
                                if (taskList.getSelectedIndex() >= 0) {
844
                                        csi.sendQueueRemove(taskList.getSelectedIndex());
845
                                }
846
                                csi.sendQueueUpdate();
847
                        } else if (source == btnMoveTaskUp) {
848
                                csi.sendQueueReorder(taskList.getSelectedIndex(), taskList.getSelectedIndex() - 1);
849
                                csi.sendQueueUpdate();
850
                        } else if (source == btnMoveTaskDown) {
851
                                csi.sendQueueReorder(taskList.getSelectedIndex(), taskList.getSelectedIndex() + 1);
852
                                csi.sendQueueUpdate();
853
                        } else if (source == btnUpdateTasks) {
854
                                csi.sendQueueUpdate();
855
                        }
856

    
857
                }
858
        }
859

    
860
        /*
861
        * DataUpdater thread.
862
        *        The purpose of this thread is to request data from the server at regular intervals.
863
        *        Types of data requested: XBee IDs, robot positions, battery levels.
864
        *
865
        */
866
        class DataUpdater extends Thread {
867
                final int DATAUPDATER_DELAY = 200;
868
                int count;
869
                public DataUpdater () {
870
                        super("Colonet DataUpdater");
871
                        count = 0;
872
                }
873

    
874
                public synchronized void run () {
875
                        while (true) {
876
                                try {
877
                                        if (csi != null && csi.isReady()) {
878
                                                // XBee ID Request
879
                                                if (count % 5 == 0) {
880
                                                        csi.sendXBeeIDRequest();
881
                                                }
882
                                                // Robot Position Request
883
                                                if (count % 1 == 0) {
884
                                                        csi.sendPositionRequest();
885
                                                }
886
                                                // Battery Request
887
                                                if (count % 30 == 0) {
888
                                                        for (Map.Entry<Integer,RobotIcon> entry : robotIcons.entrySet()) {
889
                                                                RobotIcon r = entry.getValue();
890
                                                                        int id = r.id;
891
                                                                                if (id >= 0) {
892
                                                                                csi.sendBatteryRequest(id);
893
                                                                        }
894
                                                        }
895
                                                }
896
                                        }
897
                                        count++;
898
                                        if (count > 1000)
899
                                                count = 0;
900
                                        Thread.sleep(DATAUPDATER_DELAY);
901
                                } catch (InterruptedException e) {
902
                                        return;
903
                                } catch (NullPointerException e) {
904
                                    // This probably indicates that an object was destroyed by shutdown.
905
                                    return;
906
                                }
907
                        }
908
                }
909
        }
910

    
911

    
912
        /*
913
        * WebcamPanel class
914
        * Contains the webcam image
915
        */
916
        class WebcamPanel extends JPanel {
917

    
918
                volatile BufferedImage img;
919
                BufferedImage buffer;
920

    
921
                public WebcamPanel () {
922
                        super(true);
923
                }
924

    
925
                public synchronized void setImage (BufferedImage newimg) {
926
                        if (img != null) {
927
                                img.flush();
928
                        }
929
                        System.gc();
930
                        img = newimg;
931
                        repaint();
932
                }
933

    
934
                public synchronized void paint (Graphics g) {
935
                        if (img == null) {
936
                                return;
937
                        }
938

    
939
                        // Calculate scaling
940
                        int maxWidth = getWidth() - 2*ColonetConstants.WEBCAM_BORDER;
941
                        int maxHeight = getHeight() - 2*ColonetConstants.WEBCAM_BORDER;
942
                        double widthRatio = 1.0 * maxWidth / img.getWidth();
943
                        double heightRatio = 1.0 * maxHeight / img.getHeight();
944
                        double scale = 0;
945
                        int newWidth = 0;
946
                        int newHeight = 0;
947
                        int x = 0;
948
                        int y = 0;
949

    
950
                        if (widthRatio > heightRatio) {         //height is the limiting factor
951
                                scale = heightRatio;
952
                                newHeight = maxHeight;
953
                                newWidth = (int) (img.getWidth() * scale);
954
                                y = ColonetConstants.WEBCAM_BORDER;
955
                                x = (maxWidth - newWidth) / 2 + ColonetConstants.WEBCAM_BORDER;
956
                        } else {        //width is the limiting factor
957
                                scale = widthRatio;
958
                                newWidth = maxWidth;
959
                                newHeight = (int) (img.getHeight() * scale);
960
                                x = ColonetConstants.WEBCAM_BORDER;
961
                                y = (maxHeight - newHeight) / 2 + ColonetConstants.WEBCAM_BORDER;
962
                        }
963

    
964
                        // Draw image onto the buffer
965
                        buffer = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_RGB);
966
                        Graphics2D bufferedGraphics = (Graphics2D)buffer.getGraphics();
967
                        bufferedGraphics.setColor(Color.GRAY);
968
                        bufferedGraphics.fillRect(0, 0, this.getWidth(), this.getHeight());
969
                        Image imgScaled = img.getScaledInstance(newWidth, newHeight, Image.SCALE_FAST);
970
                        bufferedGraphics.drawImage(imgScaled, x, y, this);
971

    
972
      // Draw boundary
973
      if (boundary.isSetting() || boundary.isActive()) {
974
         bufferedGraphics.setColor(Color.BLUE);
975
         int width = boundary.panel_p2.x - boundary.panel_p1.x;
976
         int height = boundary.panel_p2.y - boundary.panel_p1.y;
977
         bufferedGraphics.drawRect(boundary.panel_p1.x, boundary.panel_p1.y, width, height);
978
      }
979
      
980
      // Draw charging station
981
      if (station != null && station.isActive()) {
982
        bufferedGraphics.setStroke(new BasicStroke(2));
983
        bufferedGraphics.setColor(Color.ORANGE);
984
        int width = ColonetConstants.STATION_SIZE;
985
        bufferedGraphics.drawRect(station.getX() - width/2, station.getY() - width/2, width, width);
986
      }
987

    
988
                        // Draw Identifiers and battery levels
989
                        if (robotIcons != null) {
990
                                bufferedGraphics.setStroke(new BasicStroke(2));
991
                                for (Map.Entry<Integer,RobotIcon> entry : robotIcons.entrySet()) {
992
                                        RobotIcon r = entry.getValue();
993
                                        bufferedGraphics.setColor(r.color);
994
                                        // Identifier circle
995
                                        int px = (int) (x + r.x * scale);
996
                                        int py = (int) (y + r.y * scale);
997
                                        int radius = ColonetConstants.ROBOT_RADIUS;
998
                                        bufferedGraphics.drawOval(px-radius, py-radius, 2*radius, 2*radius);
999
                                        // ID, if applicable
1000
                                        if (r.id > 0) {
1001
                                            bufferedGraphics.setFont(new Font("arial", Font.PLAIN, 36));
1002
                                            bufferedGraphics.drawString("" + r.id, px-10, py+10);
1003
                                        }
1004
                                        // Battery
1005
                                        if (r.battery >= 0) {
1006
                                            int pixels = r.battery * ColonetConstants.BATTERY_WIDTH / 100;
1007
                                                if (r.battery > 50)
1008
                                                    bufferedGraphics.setColor(Color.GREEN);
1009
                                                else if (r.battery > 25)
1010
                                                    bufferedGraphics.setColor(Color.YELLOW);
1011
                                                else
1012
                                                    bufferedGraphics.setColor(Color.RED);
1013
                                                bufferedGraphics.fillRect(px+20, py+20, pixels, ColonetConstants.BATTERY_HEIGHT);
1014
                                                bufferedGraphics.setColor(Color.BLACK);
1015
                                                bufferedGraphics.drawRect(px+20, py+20, ColonetConstants.BATTERY_WIDTH, ColonetConstants.BATTERY_HEIGHT);
1016
                                        }
1017
                                        // If the robot has a destination, draw the vector
1018
                                        if (r.destx >= 0) {
1019
                                                bufferedGraphics.drawLine(px, py, (int)(x + r.destx * scale), (int)(y + r.desty * scale));
1020
                                        }
1021
                                }
1022
                        }
1023

    
1024
                        // Identify currently-selected robot
1025
                        RobotIcon r = robotIcons.get(selectedBot);
1026
                        if (r != null) {
1027
                                int px = (int) (x + r.x * scale);
1028
                                int py = (int) (y + r.y * scale);
1029
                                int radius = ColonetConstants.ROBOT_RADIUS;
1030
                                bufferedGraphics.setColor(Color.BLACK);
1031
                                bufferedGraphics.drawOval(px-radius-6, py-radius-6, 2*radius+12, 2*radius+12);
1032
                        }
1033

    
1034
                        //Display buffered content
1035
                        g.drawImage(buffer, 0, 0, this);
1036
                }
1037

    
1038
                /**
1039
                * Convert a click on the webcam panel to a coordinate that is consistent with the
1040
                * original size of the image that the panel contains.
1041
                */
1042
                public Point convertClick (MouseEvent e) {
1043
                        if (img == null) {
1044
                                return new Point(e.getX(), e.getY());
1045
                        }
1046

    
1047
                        // Calculate scaling
1048
                        int border = ColonetConstants.WEBCAM_BORDER;
1049
                        int clickx = e.getX();
1050
                        int clicky = e.getY();
1051
                        int maxWidth = getWidth() - 2*border;
1052
                        int maxHeight = getHeight() - 2*border;
1053
                        double widthRatio = 1.0 * maxWidth / img.getWidth();
1054
                        double heightRatio = 1.0 * maxHeight / img.getHeight();
1055
                        double scale = 0;
1056
                        int newWidth = 0;
1057
                        int newHeight = 0;
1058
                        int px = 0;
1059
                        int py = 0;
1060

    
1061
                        if (widthRatio > heightRatio) {         //height is the limiting factor
1062
                                scale = heightRatio;
1063
                                newHeight = maxHeight;
1064
                                newWidth = (int) (img.getWidth() * scale);
1065
                                py = clicky - border;
1066
                                px = clickx - border - (maxWidth - newWidth) / 2;
1067
                        } else {        //width is the limiting factor
1068
                                scale = widthRatio;
1069
                                newWidth = maxWidth;
1070
                                newHeight = (int) (img.getHeight() * scale);
1071
                                px = clickx - border;
1072
                                py = clicky - border - (maxHeight - newHeight) / 2;
1073
                        }
1074
                        py = (int) (py / scale);
1075
                        px = (int) (px / scale);
1076
                        return new Point(px, py);
1077
                }
1078
                
1079
        }
1080

    
1081
        /*
1082
        * WebcamLoader class
1083
        * Handles the loading of the webcam image.
1084
        */
1085
        class WebcamLoader extends Thread
1086
        {
1087
                URL imagePath;
1088
                MediaTracker mt;
1089
                BufferedImage image;
1090
                Random rand;
1091

    
1092
                public WebcamLoader (JApplet applet)
1093
                {
1094
                        super("ColonetWebcamLoader");
1095
                        mt = new MediaTracker(applet);
1096
                        ImageIO.setUseCache(false);
1097
                        rand = new Random();
1098
                }
1099

    
1100
                public void run ()
1101
                {
1102
                        while (true) {
1103
                                try {
1104
                                        Thread.sleep(ColonetConstants.WEBCAM_DELAY);
1105
                                        if (image != null)
1106
                                                image.flush();
1107
                                        System.gc();
1108
                                        try {
1109
                                                imagePath = new URL(ColonetConstants.WEBCAM_PATH + "?rand=" + rand.nextInt(100000));
1110
                                        } catch (MalformedURLException e) {
1111
                                                System.out.println("Malformed URL: could not form URL from: [" + ColonetConstants.WEBCAM_PATH + "]\n");
1112
                                        }
1113
                                        image = ImageIO.read(imagePath);
1114
                                        // The MediaTracker waitForID pauses the thread until the image is loaded.
1115
                                        // We don't want to display a half-downloaded image.
1116
                                        mt.addImage(image, 1);
1117
                                        while(!mt.checkID(1))
1118
                                                Thread.sleep(20);
1119
                                        mt.removeImage(image);
1120
                                        // Save
1121
                                        panelWebcam.setImage(image);
1122
                                } catch (InterruptedException e) {
1123
                                        return;
1124
                                } catch (java.security.AccessControlException e) {
1125
                                        csi.warn("Could not load webcam.\n" + e);
1126
                                        return;
1127
                                } catch (IOException e) {
1128
                                        getInfoPanel().append("IOException while trying to load image.");
1129
                                }
1130
                        }
1131
                }
1132
        }
1133
  
1134
  class RobotBoundary {
1135
    public volatile boolean active, set;
1136
    public volatile Point img_p1, img_p2;
1137
    public volatile Point panel_p1, panel_p2;
1138
    
1139
    public RobotBoundary () {
1140
      active = false;
1141
      set = false;
1142
      img_p1 = new Point(-1,-1);
1143
      img_p2 = new Point(-1,-1);
1144
      panel_p1 = new Point(-1,-1); 
1145
      panel_p2 = new Point(-1,-1);
1146
    }
1147
    
1148
    public void sendToServer () {
1149
      if (csi != null) {
1150
        if (img_p1.x == -1 || img_p1.y == -1)
1151
          csi.sendBoundaryClear();
1152
        else
1153
          csi.sendBoundary(img_p1.x, img_p1.y, img_p2.x, img_p2.y);
1154
      }
1155
    }
1156
    
1157
    public boolean isActive () {
1158
      return active;
1159
    }
1160
    
1161
    public boolean isSetting () {
1162
      return set;
1163
    }
1164
    
1165
    /**
1166
    *  Returns a Rectangle designating the active area 
1167
    *  in the coordinate system of the JPanel.
1168
    *  Returns null if the boundary is not active.
1169
    */
1170
    public Rectangle toRectangle () {
1171
      if (!active)
1172
        return null;
1173
      return new Rectangle (panel_p1.x, panel_p1.y, (panel_p2.x-panel_p1.x), (panel_p2.y-panel_p1.y));
1174
    }
1175
    
1176
    /** 
1177
    *  Determines whether a coordinate in the coordinate
1178
    *  system of the JPanel is contained within the boundary.
1179
    *  If the boundary is not active, then this method 
1180
    *  returns false.
1181
    */
1182
    public boolean contains (int px, int py) {
1183
      Rectangle rect = this.toRectangle();
1184
      if (rect == null || !this.active)
1185
        return false;
1186
      return rect.contains(px, py);
1187
    }
1188
        
1189
  }
1190
  
1191
  class ChargingStation {
1192
    private int x, y;
1193
    private boolean active;
1194
    
1195
    public ChargingStation () {
1196
      super();
1197
    }
1198
    
1199
    public ChargingStation (int x, int y) {
1200
      this();
1201
      setLocation(x, y);
1202
    }
1203
    
1204
    public int getX () {
1205
      return x;
1206
    }
1207
    
1208
    public int getY () {
1209
      return y;
1210
    }
1211
    
1212
    /**
1213
    *  Sets the center of the charging station to
1214
    *  the specified coordinate point.
1215
    */
1216
    public void setLocation (int x, int y) {
1217
      this.x = x;
1218
      this.y = y;
1219
    }
1220
    
1221
    public boolean isActive () {
1222
      return active;
1223
    }
1224
    
1225
    public void setActive (boolean status) {
1226
      this.active = status;
1227
    }
1228
    
1229
    /**
1230
    *  Determines whether a coordinate is contained within
1231
    *  the graphical area of the charging station.
1232
    *  Returns false if the charging station is not active.
1233
    */
1234
    public boolean contains (int px, int py) {
1235
      if (!active)
1236
        return false;
1237
      int width = ColonetConstants.STATION_SIZE;
1238
      Rectangle rect = new Rectangle(this.x - width/2, this.y - width/2, width, width);
1239
      return rect.contains(px, py);
1240
    }
1241
  
1242
  }
1243

    
1244
}