Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (38.7 KB)

1 32 gtress
import javax.swing.*;
2 320 gtress
import javax.swing.event.*;
3
import javax.imageio.*;
4 32 gtress
import java.awt.*;
5
import java.awt.image.*;
6
import java.awt.event.*;
7
import java.net.*;
8
import java.io.*;
9 427 gtress
import java.util.*;
10 32 gtress
11 320 gtress
/**
12 531 emarinel
* The Colonet Graphical User Interface Applet for use locally and over an internet connection.
13
* @author Gregory Tress
14 527 emarinel
*
15 531 emarinel
* 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 320 gtress
*/
18 671 gtress
public class Colonet extends JApplet implements ActionListener, MouseInputListener, KeyListener {
19 320 gtress
20 32 gtress
        // Connection
21 527 emarinel
        JTextField txtHost;
22
        JTextField txtPort;
23
        JButton btnConnect;
24 414 gtress
        JButton btnGetXBeeIDs;
25 32 gtress
        JLabel lblConnectionStatus;
26 527 emarinel
        JTextArea txtInfo;
27 32 gtress
        JPanel panelConnect;
28
        JPanel panelServerInterface;
29 527 emarinel
        Socket socket;
30
        DataUpdater dataUpdater;
31
32 32 gtress
        // Control
33
        JTabbedPane tabPaneControl;
34
        JPanel panelRobotControl;
35
        JPanel panelRobotDirection;
36 320 gtress
        JPanel panelRobotDirectionButtons;
37 32 gtress
        JPanel panelRobotCommands;
38
        JButton btnF, btnB, btnL, btnR, btnActivate;
39 320 gtress
        JLabel lblBattery;
40 428 gtress
        JLabel lblSelected;
41 333 gtress
        BatteryIcon batteryIcon;
42 320 gtress
        JPanel panelBattery;
43
        VectorController vectorController;
44
        BufferedImage imageVectorControl;
45 427 gtress
        JButton btnAssignID;
46 702 gtress
        JButton btnLocateStation;
47
        boolean setStation;
48
        ChargingStation station;
49 428 gtress
        boolean setWaypoint;
50 429 gtress
        int setWaypointID;
51 739 gtress
        JButton btnSetBounds;
52
        JButton btnClearBounds;
53
        RobotBoundary boundary;
54 428 gtress
        JButton btnCommand_MoveTo;
55 429 gtress
        JButton btnCommand_MoveAll;
56 320 gtress
        JButton btnCommand_StopTask;
57
        JButton btnCommand_ResumeTask;
58
        JButton btnCommand_ChargeNow;
59
        JButton btnCommand_StopCharging;
60 527 emarinel
61 32 gtress
        // 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 320 gtress
        JButton btnUpdateTasks;
73
        TaskAddWindow taskAddWindow;
74 527 emarinel
75 585 gtress
        // Webcam
76 320 gtress
        WebcamPanel panelWebcam;
77 32 gtress
        GraphicsConfiguration gc;
78 320 gtress
        JTabbedPane tabPaneMain;
79 585 gtress
        WebcamLoader webcamLoader;
80
81
        // Robots
82 531 emarinel
        volatile int selectedBot;         //the user has selected this bot graphically
83 671 gtress
        volatile RobotList robotIcons;         //contains boundary shapes around bots for click detection
84 320 gtress
        volatile int[] xbeeID;
85 527 emarinel
86 501 gtress
        Colonet self = this;
87 549 gtress
        volatile ColonetServerInterface csi;
88 32 gtress
89
        public void init () {
90 701 gtress
                // Set the default look and feel
91
                String laf = ColonetConstants.LOOK_AND_FEEL;
92 531 emarinel
                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 32 gtress
                // We should invoke and wait to avoid browser display difficulties
101
                Runnable r = new Runnable() {
102
                        public void run() {
103
                                createAndShowGUI();
104
                        }
105
                };
106 531 emarinel
107 32 gtress
                try {
108
                        SwingUtilities.invokeAndWait(r);
109
                } catch (InterruptedException e) {
110
                        //Not really sure why we would be in this situation
111 320 gtress
                        System.out.println("InterruptedException in init: " + e);
112 32 gtress
                } catch (java.lang.reflect.InvocationTargetException e) {
113 320 gtress
                        //This could happen for various reasons if there is a problem in createAndShowGUI
114
                        e.printStackTrace();
115 32 gtress
                }
116
        }
117 527 emarinel
118 32 gtress
        public void destroy () {
119 633 emarinel
                if (csi != null) {
120
                        csi.disconnect();
121 570 gtress
                }
122 32 gtress
        }
123
124
        private synchronized void createAndShowGUI () {
125 585 gtress
                // Webcam
126 527 emarinel
                gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
127 320 gtress
                panelWebcam = new WebcamPanel();
128
                tabPaneMain = new JTabbedPane();
129 739 gtress
                tabPaneMain.setFont(new Font("arial", Font.PLAIN, 14));
130 320 gtress
                tabPaneMain.add(panelWebcam, "Webcam");
131 527 emarinel
132 585 gtress
                // Robots
133 428 gtress
                selectedBot = -1;
134 671 gtress
                robotIcons = new RobotList();
135 527 emarinel
136 32 gtress
                // Connection area
137
                txtInfo = new JTextArea();
138
                txtInfo.setBorder(BorderFactory.createTitledBorder("Info"));
139 470 gtress
                txtHost = new JTextField(this.getDocumentBase().getHost());
140 32 gtress
                txtHost.setBorder(BorderFactory.createTitledBorder("Host"));
141
                txtPort = new JTextField("10123");
142
                txtPort.setBorder(BorderFactory.createTitledBorder("Port"));
143
                btnConnect = new JButton("Connect");
144 555 emarinel
                btnConnect.setFont(new Font("arial", Font.BOLD, 16));
145 414 gtress
                btnGetXBeeIDs = new JButton("Get XBee IDs");
146 389 gtress
                getRootPane().setDefaultButton(btnConnect);
147 32 gtress
                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 585 gtress
                panelServerInterface.add(txtInfo);
158 527 emarinel
159 32 gtress
                // Robot direction panel
160
                panelRobotDirection = new JPanel();
161 320 gtress
                panelRobotDirectionButtons = new JPanel();
162 555 emarinel
163 739 gtress
                Font f = new Font(null, Font.PLAIN, 16);
164 555 emarinel
                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 710 gtress
                btnActivate.setFont(new Font(null, Font.BOLD, 24));
174 555 emarinel
175 320 gtress
                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 527 emarinel
182 701 gtress
                imageVectorControl = gc.createCompatibleImage(ColonetConstants.VECTOR_CONTROLLER_WIDTH, ColonetConstants.VECTOR_CONTROLLER_HEIGHT);
183 638 gtress
                vectorController = new VectorController(imageVectorControl, self);
184 320 gtress
                panelRobotDirection.setLayout(new BorderLayout());
185 585 gtress
                panelRobotDirection.add(vectorController, BorderLayout.CENTER);
186 320 gtress
                panelRobotDirection.add(panelRobotDirectionButtons, BorderLayout.SOUTH);
187 527 emarinel
188 32 gtress
                // Robot Control and Commands
189
                panelRobotCommands = new JPanel();
190 739 gtress
                panelRobotCommands.setLayout(new GridLayout(6,2));
191 333 gtress
                // Battery subset
192 514 gtress
                batteryIcon = new BatteryIcon(0);
193 333 gtress
                lblBattery = new JLabel(batteryIcon);
194 428 gtress
                lblSelected = new JLabel("None");
195 664 gtress
                // Management subset
196 702 gtress
                setStation = false;
197 428 gtress
                setWaypoint = false;
198 429 gtress
                setWaypointID = -1;
199 427 gtress
                btnAssignID = new JButton("Assign ID");
200 739 gtress
                btnLocateStation = new JButton("Identify Station");
201
                boundary = new RobotBoundary();
202
                btnSetBounds = new JButton("Set Boundary");
203
                btnClearBounds = new JButton("Clear Boundary");
204
                // Control subset
205 428 gtress
                btnCommand_MoveTo = new JButton("Move to ...");
206 429 gtress
                btnCommand_MoveAll = new JButton("Move all ...");
207 320 gtress
                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 428 gtress
                panelRobotCommands.add(new JLabel("Selected Icon: "));
214
                panelRobotCommands.add(lblSelected);
215 427 gtress
                panelRobotCommands.add(btnAssignID);
216 702 gtress
    panelRobotCommands.add(btnLocateStation);
217 739 gtress
                panelRobotCommands.add(btnCommand_ChargeNow);
218
                panelRobotCommands.add(btnCommand_StopCharging);
219 664 gtress
                panelRobotCommands.add(btnSetBounds);
220 682 gtress
    panelRobotCommands.add(btnClearBounds);
221 428 gtress
                panelRobotCommands.add(btnCommand_MoveTo);
222 429 gtress
                panelRobotCommands.add(btnCommand_MoveAll);
223 427 gtress
                //panelRobotCommands.add(btnCommand_StopTask);
224
                //panelRobotCommands.add(btnCommand_ResumeTask);
225 32 gtress
                panelRobotControl = new JPanel();
226
                panelRobotControl.setLayout(new GridLayout(2,1));
227
                panelRobotControl.add(panelRobotDirection);
228
                panelRobotControl.add(panelRobotCommands);
229 527 emarinel
230 32 gtress
                // 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 320 gtress
                panelTaskManagerControls.setLayout(new GridLayout(1,4));
240 32 gtress
                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 320 gtress
                btnUpdateTasks = new JButton("Update");
247 32 gtress
                panelTaskManagerControlsPriority.add(btnMoveTaskUp);
248
                panelTaskManagerControlsPriority.add(btnMoveTaskDown);
249
                panelTaskManagerControls.add(btnAddTask);
250
                panelTaskManagerControls.add(btnRemoveTask);
251 320 gtress
                panelTaskManagerControls.add(btnUpdateTasks);
252 32 gtress
                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 320 gtress
                taskAddWindow = new TaskAddWindow();
257 527 emarinel
258 32 gtress
                // Main control mechanism
259
                tabPaneControl = new JTabbedPane(JTabbedPane.TOP);
260 739 gtress
                tabPaneControl.setFont(new Font("arial", Font.PLAIN, 14));
261 701 gtress
                tabPaneControl.setPreferredSize(new Dimension(ColonetConstants.VECTOR_CONTROLLER_WIDTH, 0));
262 32 gtress
                tabPaneControl.addTab("Connection", panelServerInterface);
263
                tabPaneControl.addTab("Robots", panelRobotControl);
264 514 gtress
                //tabPaneControl.addTab("Tasks", panelTaskManager);
265 527 emarinel
266 320 gtress
                // Put all elements in the ContentPane
267 32 gtress
                this.getContentPane().setLayout(new BorderLayout());
268 320 gtress
                this.getContentPane().add(tabPaneMain, BorderLayout.CENTER);
269 702 gtress
                this.getContentPane().add(tabPaneControl, BorderLayout.EAST);
270 32 gtress
                this.setVisible(true);
271 559 gtress
272
                // Disable components before connecting
273
                btnConnect.setText("Connect");
274 682 gtress
    lblConnectionStatus.setText("Status: Disconnected");
275 702 gtress
    setComponentsEnabled(false);
276 527 emarinel
277 320 gtress
                /* Add all listeners here */
278
                // Task Management
279 32 gtress
                btnAddTask.addActionListener(this);
280
                btnRemoveTask.addActionListener(this);
281
                btnMoveTaskUp.addActionListener(this);
282
                btnMoveTaskDown.addActionListener(this);
283 320 gtress
                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 428 gtress
                btnCommand_MoveTo.addActionListener(this);
296 429 gtress
                btnCommand_MoveAll.addActionListener(this);
297 320 gtress
                btnCommand_StopTask.addActionListener(this);
298
                btnCommand_ResumeTask.addActionListener(this);
299
                btnCommand_ChargeNow.addActionListener(this);
300
                btnCommand_StopCharging.addActionListener(this);
301
                // Other
302 32 gtress
                btnConnect.addActionListener(this);
303 414 gtress
                btnGetXBeeIDs.addActionListener(this);
304 427 gtress
                btnAssignID.addActionListener(this);
305 702 gtress
                btnLocateStation.addActionListener(this);
306 682 gtress
    btnSetBounds.addActionListener(this);
307
    btnClearBounds.addActionListener(this);
308 527 emarinel
                panelWebcam.addMouseListener(this);
309 670 gtress
                panelWebcam.addMouseMotionListener(this);
310 32 gtress
        }
311 638 gtress
312
        public ColonetServerInterface getCSI () {
313 739 gtress
          return csi;
314 638 gtress
        }
315 527 emarinel
316 428 gtress
        public void paint (Graphics g) {
317 531 emarinel
                super.paint(g);
318