/
GUIFrame.java
593 lines (520 loc) · 20.7 KB
/
GUIFrame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/** GUIFrame.java */
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*; // for enumeration and vector
/** <B>GUIFrame</B> - Java-1.2/Swing based GUI for simulator MiniSim.
* See the MiniSim description for more info.
*
* <I>Written by
* <A HREF="mailto:aganse@apl.washington.edu">Andy Ganse</A>,
* Applied Physics Lab, University of Washington, copyright 1999 by UW.
* </I>
*
* <P>
* @see MiniSim
*/
public class GUIFrame implements ActionListener {
double pi = java.lang.Math.PI;
boolean frozen = true;
boolean simRegistered = false;
boolean simStarted = false;
String buoyModel = new String("LOFAR");
JSlider st;
JTextField fednameTxt;
JButton startPauseSimButton;
MapPane mapPane;
double initLat=20, initLon=20;
JTextArea comMsgTxt;
//JScrollPane comMsgScrl;
JTextArea diagTxt;
JScrollPane diagScrl;
javax.swing.Timer animTimer;
int frameNumber = -1;
SimInterface sim;
private SimRegisThread regThread = null;
private SimStartThread startThread = null;
public GUIFrame(Container container, Image seaflrImage,
Image subImage, Image buoyImage, SimInterface newSim){
/** Connect the handle to the simulation interface (note that's not
* a java interface) **/
this.sim = newSim;
/** Hardwired maxSimTime here, but could be made user-settable */
int maxSimTime = 120;
this.sim.setMaxSimTime(maxSimTime);
/** Create and set the animation timer */
int fps = 10;
// Set how many milliseconds between frames
int delay = (fps > 0) ? (1000 / fps) : 100;
// Set up a timer that calls this object's action handler
animTimer = new javax.swing.Timer(delay, this);
animTimer.setInitialDelay(0);
animTimer.setCoalesce(true);
/** Create the simTime panel: */
JPanel simTimePane = new JPanel();
st = new JSlider(JSlider.HORIZONTAL, 0, maxSimTime, 0);
st.setValue(0);
st.setEnabled(false);
st.setPaintTicks(true);
st.setPaintLabels(true);
st.setMajorTickSpacing(maxSimTime/4);
st.setMinorTickSpacing(maxSimTime/8);
simTimePane.add(st, BorderLayout.EAST);
simTimePane.setBorder(new
TitledBorder(BorderFactory.createEtchedBorder(), "Simulation Time"));
/** Create the subName panel: */
JPanel subNamePane = new JPanel();
fednameTxt = new JTextField("USS Foo", 16);
subNamePane.add(fednameTxt);
subNamePane.setBorder(new
TitledBorder(BorderFactory.createEtchedBorder(), "Submarine Name"));
/** Create the buttons panel: */
JPanel buttonsPane = new JPanel();
buttonsPane.setLayout(new GridLayout(2,2));
JButton regisFedButton = new JButton("Register Sim");
regisFedButton.addActionListener(new RegisBtnLsnr());
buttonsPane.add(regisFedButton);
JButton scuttleButton = new JButton("Scuttle");
scuttleButton.addActionListener(new ScuttleBtnLsnr());
buttonsPane.add(scuttleButton);
startPauseSimButton = new JButton("Start Sim");
startPauseSimButton.addActionListener(new StartPauseBtnLsnr());
buttonsPane.add(startPauseSimButton);
JButton exitSimButton = new JButton("Exit");
exitSimButton.addActionListener(new ExitBtnLsnr());
buttonsPane.add(exitSimButton);
buttonsPane.setBorder(new
TitledBorder(BorderFactory.createEtchedBorder(),
"Simulation Control"));
/** Create the sonobuoy-type panel: */
JPanel buoyPane = new JPanel();
buoyPane.setLayout(new GridLayout(2,2));
buoyPane.setBorder(new
TitledBorder(BorderFactory.createEtchedBorder(), "Sonobuoy Type"));
ButtonGroup buoyGroup = new ButtonGroup();
JRadioButton LOFARbtn = new JRadioButton("LOFAR");
LOFARbtn.setSelected(true);
JRadioButton DICASSbtn = new JRadioButton("DICASS");
JRadioButton DIFARbtn = new JRadioButton("DIFAR");
JRadioButton VLADbtn = new JRadioButton("VLAD");
LOFARbtn.addActionListener(new LOFARlsnr());
DICASSbtn.addActionListener(new DICASSlsnr());
DIFARbtn.addActionListener(new DIFARlsnr());
VLADbtn.addActionListener(new VLADlsnr());
buoyGroup.add(LOFARbtn);
buoyGroup.add(DICASSbtn);
buoyGroup.add(DIFARbtn);
buoyGroup.add(VLADbtn);
buoyPane.add(LOFARbtn);
buoyPane.add(DICASSbtn);
buoyPane.add(DIFARbtn);
buoyPane.add(VLADbtn);
/** Create the leftControl panel: */
JPanel leftConPane = new JPanel();
leftConPane.setLayout(new GridLayout(2,1));
leftConPane.add(subNamePane, BorderLayout.NORTH);
leftConPane.add(buoyPane, BorderLayout.SOUTH);
/** Create the rightControl panel: */
JPanel rightConPane = new JPanel();
rightConPane.setLayout(new GridLayout(2,1));
rightConPane.add(buttonsPane, BorderLayout.NORTH);
rightConPane.add(simTimePane, BorderLayout.SOUTH);
/** Create the more general controlsPanel and add leftControl-panel
* and rightControl-panel to it: */
JPanel controlsPane = new JPanel();
controlsPane.setLayout(new GridLayout(1,2));
controlsPane.add(leftConPane, BorderLayout.WEST);
controlsPane.add(rightConPane, BorderLayout.EAST);
/** Create the message panel: */
JPanel msgPane = new JPanel();
msgPane.setBorder(new
TitledBorder(BorderFactory.createEtchedBorder(), "Comms Messages"));
comMsgTxt = new JTextArea("",1,38);
comMsgTxt.setEditable(false);
//comMsgScrl = new JScrollPane(comMsgTxt);
//msgPane.add(comMsgScrl);
msgPane.add(comMsgTxt);
/** Create the status panel: */
JPanel statusPane = new JPanel();
statusPane.setBorder(new
TitledBorder(BorderFactory.createEtchedBorder(),
"Status/Diagnostic Messages"));
diagTxt = new JTextArea("Status/Diagnostic messages will display here:",
3, 38);
diagTxt.setEditable(false);
diagScrl = new JScrollPane(diagTxt);
statusPane.add(diagScrl);
/** Create the directions panel: */
JPanel dirsPane = new JPanel();
dirsPane.setBorder(new
TitledBorder(BorderFactory.createEtchedBorder(), "Directions for Use"));
dirsPane.setLayout(new GridLayout(6,1));
dirsPane.add(new
JLabel(" 1.) Enter your own name in Submarine Name field."));
dirsPane.add(new
JLabel(" 2.) Click Register Sim button to register simulation."));
dirsPane.add(new
JLabel(" 3.) Click Start button to start simulation."));
dirsPane.add(new
JLabel(" 4.) Choose Sonobuoy Types and click on map to drop them in."));
dirsPane.add(new
JLabel(" 5.) Click Scuttle button to clear sonobuoys."));
dirsPane.add(new
JLabel(" 6.) To exit click Exit button or the window's X button."));
/** Create the map panel: */
mapPane = new MapPane(seaflrImage, subImage, buoyImage);
JScrollPane mapScrollPane = new JScrollPane(mapPane);
/** Create the bottom panel: */
JPanel bottomPane = new JPanel();
bottomPane.setLayout(new BorderLayout());
bottomPane.add(msgPane, BorderLayout.NORTH);
//bottomPane.add(statusPane, BorderLayout.SOUTH);
bottomPane.add(dirsPane, BorderLayout.SOUTH);
/** Place all the panels onto the GUIFrame: */
container.add(controlsPane, BorderLayout.NORTH);
container.add(mapScrollPane, BorderLayout.CENTER);
container.add(bottomPane, BorderLayout.SOUTH);
}
/** Start the animation updates in the mapPane and simTime slider */
public synchronized void startAnimation() {
if (!frozen) {
// Start animating!
if (!animTimer.isRunning()) {
animTimer.start();
}
}
}
/** Stop the animation updates in the mapPane and simTime slider */
public synchronized void stopAnimation() {
// Stop the animating thread.
if (animTimer.isRunning()) {
animTimer.stop();
}
}
/** Trap actionEvents on GUIFrame object (fired by Timer) */
public void actionPerformed(ActionEvent e) {
//Advance animation frame
frameNumber++;
// Update display
mapPane.repaint();
}
/** Appends a note to the Diagnostic Output textbox on the GUI */
public void diagOut(String diagString) {
diagTxt.append("\n");
diagTxt.append(diagString);
diagScrl.getVerticalScrollBar().setValue(
diagScrl.getVerticalScrollBar().getMaximum() );
}
/** Appends a 'comms' message to the Comms Msgs textbox on the GUI */
public void displayComMsg(String comString) {
comMsgTxt.setText("lastest msg: " + comString);
//comMsgTxt.append(comString + "\n");
//comMsgScrl.getVerticalScrollBar().setValue(
// comMsgScrl.getVerticalScrollBar().getMaximum() );
}
/** This is the map area of the applet where the action takes place.
* Each repaint of this panel is one frame of the map "animation". */
class MapPane extends JPanel {
Image subImg, buoyImg, seafloorImg;
LatLonGrid viewgrid = new LatLonGrid(0,40,0,40);
public MapPane(Image seafloorImg, Image subImg, Image buoyImg) {
this.seafloorImg = seafloorImg;
this.subImg = subImg;
this.buoyImg = buoyImg;
setBorder(BorderFactory.createEtchedBorder());
setCursor( Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR) );
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
/** add a buoy where mouse was clicked */
e.consume();
if (simRegistered && simStarted) {
sim.addBuoy(
( ((double)(e.getX())/(double)(getWidth())) *
(double)(viewgrid.maxlon-viewgrid.minlon) +
(double)(viewgrid.minlon) ),
( ((double)(e.getY())/(double)(getHeight())) *
(double)(viewgrid.maxlat-viewgrid.minlat) +
(double)(viewgrid.minlat) ),
buoyModel);
}
}
});
}
/** Draw the current frame of map. */
public void paintComponent(Graphics g1) {
Graphics2D g = (Graphics2D)g1;
super.setBackground(Color.darkGray);
super.paintComponent(g); //paint any space not covered
//by the seafloor image
int mapPaneWidth = getWidth();
int mapPaneHeight = getHeight();
int seaflrImgWidth, seaflrImgHeight;
int subImgWidth, subImgHeight;
int buoyImgWidth, buoyImgHeight;
int x, y;
double simTime;
/** Get sim data from sim execution */
simTime = (sim==null)?0.:sim.getSimTime();
Vector navalObjectsList = sim.getNavalObjects();
/** Get image params */
seaflrImgWidth = seafloorImg.getWidth(this);
seaflrImgHeight = seafloorImg.getHeight(this);
subImgWidth = subImg.getWidth(this);
subImgHeight = subImg.getHeight(this);
buoyImgWidth = buoyImg.getWidth(this);
buoyImgHeight = buoyImg.getHeight(this);
/** Draw seafloor image on map panel */
if ((seaflrImgWidth > 0) && (seaflrImgHeight > 0)) {
g.drawImage(seafloorImg,
(mapPaneWidth - seaflrImgWidth)/2,
(mapPaneHeight - seaflrImgHeight)/2, this);
}
/** Draw lat lines every 10 degrees */
for(y=(int)viewgrid.minlat; y<=(int)viewgrid.maxlat; y++) {
if( y%10==0 ) {
g.drawLine(
(int)( ((double)mapPaneWidth-(double)seaflrImgWidth)/2 ),
(int)( (double)y / (viewgrid.maxlat-viewgrid.minlat) *
(double)seaflrImgHeight+
((double)mapPaneHeight-(double)seaflrImgHeight)/2 ),
(int)( (double)seaflrImgWidth +
((double)mapPaneWidth-(double)seaflrImgWidth)/2 ),
(int)( (double)y / (viewgrid.maxlat-viewgrid.minlat) *
(double)seaflrImgHeight+
((double)mapPaneHeight-(double)seaflrImgHeight)/2 )
);
g.setColor(Color.black);
g.drawString(String.valueOf(y),
(int)( ((double)mapPaneWidth-(double)seaflrImgWidth)/2 ) + 5,
(int)( (double)y / (viewgrid.maxlat-viewgrid.minlat) *
(double)seaflrImgHeight+
((double)mapPaneHeight-(double)seaflrImgHeight)/2 )
);
}
}
/** Draw lon lines every 10 degrees */
for(x=(int)viewgrid.minlon; x<=(int)viewgrid.maxlon; x++) {
if( x%10==0 ) {
g.drawLine(
(int)( (double)x / (viewgrid.maxlon-viewgrid.minlon) *
(double)seaflrImgWidth+
((double)mapPaneWidth-(double)seaflrImgWidth)/2 ),
(int)( ((double)mapPaneHeight-(double)seaflrImgHeight)/2 ),
(int)( (double)x / (viewgrid.maxlon-viewgrid.minlon) *
(double)seaflrImgWidth+
((double)mapPaneWidth-(double)seaflrImgWidth)/2 ),
(int)( (double)seaflrImgHeight +
((double)mapPaneHeight-(double)seaflrImgHeight)/2 )
);
g.setColor(Color.black);
g.drawString(String.valueOf(x),
(int)( (double)x / (viewgrid.maxlon-viewgrid.minlon) *
(double)seaflrImgWidth+
((double)mapPaneWidth-(double)seaflrImgWidth)/2 ),
(int)( ((double)mapPaneHeight-(double)seaflrImgHeight)/2 )+10
);
}
}
/** Draw sub(s) and sonobuoy(s) on map panel */
Enumeration navalObjEnum = navalObjectsList.elements();
while (navalObjEnum.hasMoreElements()) {
NavalObject navObj = (NavalObject)navalObjEnum.nextElement();
double lat = navObj.getLat();
double lon = navObj.getLon();
String msg = navObj.getLatestComm();
if(msg!=null) displayComMsg(msg);
if(navObj instanceof Submarine) {
Submarine sub = (Submarine)navObj;
double subx =
(int)(lon/(viewgrid.maxlon-viewgrid.minlon)*
(double)seaflrImgWidth +
((double)mapPaneWidth-
(double)seaflrImgWidth)/2 -
(double)subImgWidth/2 );
double suby =
(int)(lat/(viewgrid.maxlat-viewgrid.minlat)*
(double)seaflrImgHeight+
((double)mapPaneHeight-
(double)seaflrImgHeight)/2 -
(double)subImgHeight/2 );
AffineTransform at = new AffineTransform();
at.translate(subx,suby);
at.rotate( sub.getHeading()*2*pi/360,
(double)subImgWidth/2, (double)subImgHeight/2 );
g.drawImage(subImg, at, this);
g.setColor(Color.white);
g.drawString(sub.getName(),
(int)(lon/(viewgrid.maxlon-viewgrid.minlon)*
(double)seaflrImgWidth +
((double)mapPaneWidth-
(double)seaflrImgWidth)/2 -
(double)subImgWidth/2 )+23,
(int)(lat/(viewgrid.maxlat-viewgrid.minlat)*
(double)seaflrImgHeight+
((double)mapPaneHeight-
(double)seaflrImgHeight)/2 -
(double)subImgHeight/2 )+18);
}
if(navObj instanceof Sonobuoy) {
Sonobuoy buoy = (Sonobuoy)navObj;
g.drawImage(buoyImg,
(int)(lon/(viewgrid.maxlon-viewgrid.minlon)*
(double)seaflrImgWidth +
((double)mapPaneWidth-
(double)seaflrImgWidth)/2 -
(double)buoyImgWidth/2 ),
(int)(lat/(viewgrid.maxlat-viewgrid.minlat)*
(double)seaflrImgHeight+
((double)mapPaneHeight-
(double)seaflrImgHeight)/2 -
(double)buoyImgHeight/2 ),
this);
g.setColor(Color.white);
g.drawString(Integer.toString(buoy.getIDNum()) +
" ("+buoy.getModel()+")",
(int)(lon/(viewgrid.maxlon-viewgrid.minlon)*
(double)seaflrImgWidth +
((double)mapPaneWidth-
(double)seaflrImgWidth)/2 -
(double)buoyImgWidth/2 )+5,
(int)(lat/(viewgrid.maxlat-viewgrid.minlat)*
(double)seaflrImgHeight+
((double)mapPaneHeight-
(double)seaflrImgHeight)/2 -
(double)buoyImgHeight/2 ));
}
}
/** Update simTime slider position. */
st.setValue((int)simTime);
/** Check for "simOver" - if over then display & stop animation */
if(sim.isOver()) {
animTimer.stop();
g.setColor(Color.red);
g.drawString("SIMULATION OVER",
(int)(0.39*(double)seaflrImgWidth +
((double)mapPaneWidth-(double)seaflrImgWidth)/2),
(int)(0.39*(double)seaflrImgHeight+
((double)mapPaneHeight-(double)seaflrImgHeight)/2) );
g.setColor(Color.white);
}
}
/** Contains map panel lat-lon boundaries which can be changed */
public class LatLonGrid {
public double minlat;
public double maxlat;
public double minlon;
public double maxlon;
LatLonGrid (double lon1, double lon2, double lat1, double lat2) {
minlat=lat1;
maxlat=lat2;
minlon=lon1;
maxlon=lon2;
if(minlat>maxlat || minlon>maxlon) {
System.out.println(
"MiniSim: LatLonGrid: error: screwy entry in grid bounds\n");
System.exit(1);
}
}
}
}
/** Button listener for LOFAR radio button */
class LOFARlsnr implements ActionListener {
public void actionPerformed(ActionEvent e) {
buoyModel="LOFAR";
}
}
/** Button listener for DIFAR radio button */
class DIFARlsnr implements ActionListener {
public void actionPerformed(ActionEvent e) {
buoyModel="DIFAR";
}
}
/** Button listener for DICASS radio button */
class DICASSlsnr implements ActionListener {
public void actionPerformed(ActionEvent e) {
buoyModel="DICASS";
}
}
/** Button listener for VLAD radio button */
class VLADlsnr implements ActionListener {
public void actionPerformed(ActionEvent e) {
buoyModel="VLAD";
}
}
/** Button listener for regis button */
class RegisBtnLsnr implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (!simRegistered && !simStarted) {
displayComMsg("Sim registered with user " +
fednameTxt.getText() + ".");
diagOut("registering sim...");
fednameTxt.setEnabled(false);
sim.setSubInitValues(fednameTxt.getText(),initLon,initLat);
simRegistered=true;
if(regThread==null) regThread = new SimRegisThread();
mapPane.repaint();
}
}
}
/** Button listener for start/pause button */
class StartPauseBtnLsnr implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (frozen) {
frozen = false;
if (simRegistered && !simStarted) { // starting for first time
diagOut("starting sim");
displayComMsg("Sim started.");
startPauseSimButton.setText("Pause");
simStarted=true;
if(startThread==null) startThread = new SimStartThread();
startAnimation();
} else if (simRegistered && simStarted) { // return from pause
displayComMsg("Resuming sim animation.");
startPauseSimButton.setText("Pause");
startAnimation();
} else {
displayComMsg("Must first register sim.");
}
} else { // pause animation
frozen = true;
displayComMsg("Sim animation paused (sim itself still going)");
startPauseSimButton.setText("Unpause");
stopAnimation();
}
}
}
/** Button listener for scuttle button */
class ScuttleBtnLsnr implements ActionListener {
public void actionPerformed(ActionEvent e) {
displayComMsg("Sonobuoys scuttled!");
diagOut("scuttling buoys");
if(sim!=null) sim.scuttleBuoys();
}
}
/** Button listener for exit button */
class ExitBtnLsnr implements ActionListener {
public void actionPerformed(ActionEvent e) {
stopAnimation();
displayComMsg("Closing simulation.");
//destroy();
System.exit(0);
}
}
/** Thread-handling class for registering sim without freezing up the UI */
private class SimRegisThread extends Thread {
SimRegisThread() { this.start(); }
public void run() {
sim.register();
}
}
/** Thread-handling class for starting sim without freezing up the UI */
private class SimStartThread extends Thread {
SimStartThread() { this.start(); }
public void run() {
sim.start();
}
}
}