Menu

[01c4ec]: / MainWindow.java  Maximize  Restore  History

Download this file

978 lines (878 with data), 33.7 kB

  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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
/* Copyright (C) 2006-2010 Joan Queralt Molina
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
package biogenesis;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.util.*;
public class MainWindow extends JFrame {
private static final long serialVersionUID = Utils.FILE_VERSION;
protected VisibleWorld _visibleWorld;
protected World _world;
protected boolean _isProcessActive=false;
protected transient java.util.Timer _timer;
protected transient TimerTask updateTask = null;
protected JFileChooser worldChooser = new JFileChooser();
protected JFileChooser geneticCodeChooser = new JFileChooser();
protected File _gameFile = null;
protected JScrollPane scrollPane;
protected StdAction newGameAction;
protected StartStopAction startStopAction;
protected StdAction saveGameAction;
protected StdAction increaseCO2Action;
protected StdAction decreaseCO2Action;
protected StdAction manageConnectionsAction;
protected StdAction abortTrackingAction;
protected StdAction openGameAction;
protected StdAction saveGameAsAction;
protected StdAction quitAction;
protected StdAction statisticsAction;
protected StdAction labAction;
protected StdAction killAllAction;
protected StdAction disperseAllAction;
protected StdAction parametersAction;
protected StdAction aboutAction;
protected StdAction manualAction;
protected StdAction checkLastVersionAction;
protected StdAction netConfigAction;
protected JMenuItem _menuStartStopGame;
protected JMenu _menuGame;
protected JMenu _menuWorld;
protected JMenu _menuHelp;
protected JMenu _menuNet;
protected NumberFormat _nf;
protected JLabel _statusLabel;
protected JToolBar toolBar = new JToolBar(Messages.getString("T_PROGRAM_NAME")); //$NON-NLS-1$
protected InfoToolbar infoToolbar;
private String statusMessage=""; //$NON-NLS-1$
private StringBuilder statusLabelText = new StringBuilder(100);
protected Organism _trackedOrganism = null;
protected transient NetServerThread serverThread = null;
protected StatisticsWindow _statisticsWindow = null;
// Comptador de frames, per saber quan actualitzar la finestra d'informaci�
protected long nFrames=0;
private ImageIcon imageIcon = new ImageIcon(getClass().getResource("images/bullet.jpg")); //$NON-NLS-1$
public JFileChooser getWorldChooser() {
return worldChooser;
}
public JFileChooser getGeneticCodeChooser() {
return geneticCodeChooser;
}
public void setStatusMessage(String str) {
statusMessage = str;
updateStatusLabel();
}
public String getStatusMessage() {
return statusMessage;
}
public World getWorld() {
return _world;
}
public VisibleWorld getVisibleWorld() {
return _visibleWorld;
}
public boolean isProcessActive() {
return _isProcessActive;
}
public InfoToolbar getInfoPanel() {
return infoToolbar;
}
public MainWindow() {
createActions();
createMenu();
createToolBar();
setControls();
configureApp();
_world = new World(_visibleWorld);
startApp();
_world.genesis();
scrollPane.setViewportView(_visibleWorld);
}
/**
* @param args
*/
public static void main(String[] args) {
if (args.length > 1) {
System.err.println("java -jar biogenesis.jar [random seed]");
} else if (args.length == 1) {
try {
long seed = Long.parseLong(args[0]);
Utils.random.setSeed(seed);
} catch (NumberFormatException e) {
System.err.println("java -jar biogenesis.jar [random seed]");
}
}
Utils.readPreferences();
new MainWindow();
}
private void createActions() {
newGameAction = new NewGameAction("T_NEW", "images/menu_new.png", //$NON-NLS-1$ //$NON-NLS-2$
"T_NEW_WORLD"); //$NON-NLS-1$
startStopAction = new StartStopAction("T_RESUME","T_PAUSE","images/menu_start.png", //$NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
"images/menu_stop.png","T_RESUME_PROCESS","T_PAUSE_PROCESS"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
saveGameAction = new SaveGameAction("T_SAVE", "images/menu_save.png", //$NON-NLS-1$//$NON-NLS-2$
"T_SAVE_WORLD"); //$NON-NLS-1$
increaseCO2Action = new IncreaseCO2Action("T_INCREASE_CO2", "images/menu_increase_co2.png", //$NON-NLS-1$ //$NON-NLS-2$
"T_INCREASE_CO2"); //$NON-NLS-1$
decreaseCO2Action = new DecreaseCO2Action("T_DECREASE_CO2", "images/menu_decrease_co2.png", //$NON-NLS-1$ //$NON-NLS-2$
"T_DECREASE_CO2"); //$NON-NLS-1$
manageConnectionsAction = new ManageConnectionsAction("T_MANAGE_CONNECTIONS", "images/menu_manage_connections.png", //$NON-NLS-1$//$NON-NLS-2$
"T_MANAGE_CONNECTIONS"); //$NON-NLS-1$
abortTrackingAction = new AbortTrackingAction("T_ABORT_TRACKING", "images/menu_stop_tracking.png", //$NON-NLS-1$//$NON-NLS-2$
"T_ABORT_TRACKING"); //$NON-NLS-1$
openGameAction = new OpenGameAction("T_OPEN", null, "T_OPEN_WORLD"); //$NON-NLS-1$//$NON-NLS-2$
saveGameAsAction = new SaveGameAsAction("T_SAVE_AS", null, "T_SAVE_WORLD_AS"); //$NON-NLS-1$//$NON-NLS-2$
quitAction = new QuitAction("T_QUIT", null, "T_QUIT_PROGRAM"); //$NON-NLS-1$ //$NON-NLS-2$
statisticsAction = new StatisticsAction("T_STATISTICS", null, "T_WORLD_STATISTICS"); //$NON-NLS-1$ //$NON-NLS-2$
labAction = new LabAction("T_GENETIC_LABORATORY", null, "T_GENETIC_LABORATORY"); //$NON-NLS-1$ //$NON-NLS-2$
killAllAction = new KillAllAction("T_KILL_ALL", null, "T_KILL_ALL_ORGANISMS"); //$NON-NLS-1$ //$NON-NLS-2$
disperseAllAction = new DisperseAllAction("T_DISPERSE_ALL", null, "T_DISPERSE_ALL_DEAD_ORGANISMS"); //$NON-NLS-1$ //$NON-NLS-2$
parametersAction = new ParametersAction("T_PARAMETERS", null, "T_EDIT_PARAMETERS"); //$NON-NLS-1$ //$NON-NLS-2$
aboutAction = new AboutAction("T_ABOUT", null, "T_ABOUT"); //$NON-NLS-1$//$NON-NLS-2$
manualAction = new ManualAction("T_USER_MANUAL", null, "T_USER_MANUAL"); //$NON-NLS-1$//$NON-NLS-2$
checkLastVersionAction = new CheckLastVersionAction("T_CHECK_LAST_VERSION", null, "T_CHECK_LAST_VERSION"); //$NON-NLS-1$ //$NON-NLS-2$
netConfigAction = new NetConfigAction("T_CONFIGURE_NETWORK", null, "T_CONFIGURE_NETWORK"); //$NON-NLS-1$ //$NON-NLS-2$
}
public void createToolBar() {
toolBar.removeAll();
toolBar.add(newGameAction);
toolBar.add(startStopAction);
toolBar.add(saveGameAction);
toolBar.add(increaseCO2Action);
toolBar.add(decreaseCO2Action);
toolBar.add(manageConnectionsAction);
toolBar.add(abortTrackingAction);
abortTrackingAction.setEnabled(_trackedOrganism != null);
toolBar.invalidate();
toolBar.repaint();
}
private void createMenu() {
JMenuItem menuItem;
JMenuBar menuBar = new JMenuBar();
_menuGame = new JMenu(Messages.getString("T_GAME")); //$NON-NLS-1$
_menuGame.setMnemonic(Messages.getMnemonic("T_GAME").intValue()); //$NON-NLS-1$
menuBar.add(_menuGame);
menuItem = new JMenuItem(newGameAction);
menuItem.setIcon(null);
_menuGame.add(menuItem);
_menuStartStopGame = new JMenuItem(startStopAction);
_menuStartStopGame.setIcon(null);
_menuGame.add(_menuStartStopGame);
_menuGame.add(new JMenuItem(openGameAction));
menuItem = new JMenuItem(saveGameAction);
menuItem.setIcon(null);
_menuGame.add(menuItem);
_menuGame.add(new JMenuItem(saveGameAsAction));
_menuGame.add(new JMenuItem(quitAction));
_menuWorld = new JMenu(Messages.getString("T_WORLD")); //$NON-NLS-1$
_menuWorld.setMnemonic(Messages.getMnemonic("T_WORLD").intValue()); //$NON-NLS-1$
menuBar.add(_menuWorld);
_menuWorld.add(new JMenuItem(statisticsAction));
_menuWorld.add(new JMenuItem(labAction));
menuItem = new JMenuItem(increaseCO2Action);
menuItem.setIcon(null);
_menuWorld.add(menuItem);
menuItem = new JMenuItem(decreaseCO2Action);
menuItem.setIcon(null);
_menuWorld.add(menuItem);
_menuWorld.add(new JMenuItem(killAllAction));
_menuWorld.add(new JMenuItem(disperseAllAction));
_menuWorld.add(new JMenuItem(parametersAction));
_menuNet = new JMenu(Messages.getString("T_NETWORK")); //$NON-NLS-1$
_menuNet.setMnemonic(Messages.getMnemonic("T_NETWORK").intValue()); //$NON-NLS-1$
menuBar.add(_menuNet);
_menuNet.add(new JMenuItem(netConfigAction));
menuItem = new JMenuItem(manageConnectionsAction);
menuItem.setIcon(null);
_menuNet.add(menuItem);
_menuHelp = new JMenu(Messages.getString("T_HELP")); //$NON-NLS-1$
_menuHelp.setMnemonic(Messages.getMnemonic("T_HELP").intValue()); //$NON-NLS-1$
menuBar.add(_menuHelp);
_menuHelp.add(new JMenuItem(manualAction));
_menuHelp.add(new JMenuItem(checkLastVersionAction));
_menuHelp.add(new JMenuItem(aboutAction));
// Only enable file management menu options if at least there is
//permission to read user's home directory
SecurityManager sec = System.getSecurityManager();
try {
if (sec != null)
sec.checkPropertyAccess("user.home"); //$NON-NLS-1$
} catch (SecurityException ex) {
openGameAction.setEnabled(false);
saveGameAsAction.setEnabled(false);
saveGameAction.setEnabled(false);
manualAction.setEnabled(false);
netConfigAction.setEnabled(false);
manageConnectionsAction.setEnabled(false);
}
setJMenuBar(menuBar);
}
protected void checkLastVersion() {
CheckVersionThread thread = new CheckVersionThread(this);
thread.start();
}
protected void netConfig() {
new NetConfigWindow(this);
}
public void setAcceptConnections(boolean newAcceptConnections) {
if (newAcceptConnections != Utils.ACCEPT_CONNECTIONS) {
Utils.ACCEPT_CONNECTIONS = newAcceptConnections;
if (newAcceptConnections)
startServer();
else
if (serverThread.getConnections().isEmpty())
stopServer();
}
}
public boolean isAcceptingConnections() {
return Utils.ACCEPT_CONNECTIONS;
}
public NetServerThread getNetServer() {
return serverThread;
}
public NetServerThread startServer() {
if (serverThread == null || !serverThread.isActive()) {
serverThread = new NetServerThread(this);
serverThread.start();
}
return serverThread;
}
public void stopServer() {
if (serverThread != null) {
serverThread.closeServer();
}
}
class NewGameAction extends StdAction {
private static final long serialVersionUID = 1L;
public NewGameAction(String text_key, String icon_path, String desc) {
super(text_key, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
_trackedOrganism = null;
_world.genesis();
scrollPane.setViewportView(_visibleWorld);
_isProcessActive = true;
startStopAction.setActive(true);
_menuStartStopGame.setIcon(null);
setStatusMessage(Messages.getString("T_NEW_WORLD_CREATED")); //$NON-NLS-1$
}
}
class StartStopAction extends StdAction {
private static final long serialVersionUID = 1L;
protected String name2;
protected String description2;
protected Integer mnemonic2;
protected ImageIcon icon2;
protected String name2_key;
protected String desc2_key;
protected boolean active;
public StartStopAction(String text1, String text2, String icon_path1,
String icon_path2, String desc1, String desc2) {
super(text1, icon_path1, desc1);
name2 = Messages.getString(text2);
description2 = Messages.getString(desc2);
icon2 = createIcon(icon_path2);
mnemonic2 = Messages.getMnemonic(text2);
name2_key = text2;
desc2_key = desc2;
active = false;
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(Messages.getString("T_PAUSE_ACCELERATOR")));
}
public void actionPerformed(ActionEvent e) {
if (_isProcessActive)
pauseGame();
else
playGame();
}
public void toogle() {
String aux;
ImageIcon auxicon;
Integer auxmnemonic;
aux = (String) getValue(NAME);
putValue(NAME, name2);
name2 = aux;
aux = (String) getValue(SHORT_DESCRIPTION);
putValue(SHORT_DESCRIPTION, description2);
description2 = aux;
auxicon = (ImageIcon) getValue(SMALL_ICON);
putValue(SMALL_ICON, icon2);
icon2 = auxicon;
auxmnemonic = (Integer) getValue(MNEMONIC_KEY);
putValue(MNEMONIC_KEY, mnemonic2);
mnemonic2 = auxmnemonic;
active = !active;
}
@Override
public void changeLocale() {
super.changeLocale();
name2 = Messages.getString(name2_key);
description2 = Messages.getString(desc2_key);
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(Messages.getString("T_PAUSE_ACCELERATOR")));
}
public void setActive(boolean newState) {
if (newState != active) {
toogle();
active = newState;
}
}
public boolean isActive() {
return active;
}
}
class SaveGameAction extends StdAction {
private static final long serialVersionUID = 1L;
public SaveGameAction(String text, String icon_path, String desc) {
super(text, icon_path, desc);
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(Messages.getString("T_SAVE_ACCELERATOR")));
}
public void actionPerformed(ActionEvent e) {
if (_gameFile != null)
saveObject(_world, _gameFile);
else
saveGameAs();
}
@Override
public void changeLocale() {
super.changeLocale();
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(Messages.getString("T_SAVE_ACCELERATOR")));
}
}
class IncreaseCO2Action extends StdAction {
private static final long serialVersionUID = 1L;
public IncreaseCO2Action(String text, String icon_path, String desc) {
super(text, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
_world.addCO2(500);
}
}
class DecreaseCO2Action extends StdAction {
private static final long serialVersionUID = 1L;
public DecreaseCO2Action(String text, String icon_path, String desc) {
super(text, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
_world.decreaseCO2(500);
}
}
class ManageConnectionsAction extends StdAction {
private static final long serialVersionUID = 1L;
public ManageConnectionsAction(String text, String icon_path, String desc) {
super(text, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
netConnectionsWindow();
}
}
class AbortTrackingAction extends StdAction {
private static final long serialVersionUID = 1L;
public AbortTrackingAction(String text, String icon_path, String desc) {
super(text, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
setTrackedOrganism(null);
}
}
class OpenGameAction extends StdAction {
private static final long serialVersionUID = 1L;
public OpenGameAction(String text, String icon_path, String desc) {
super(text, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
boolean processState = _isProcessActive;
_isProcessActive = false;
try {
int returnVal = getWorldChooser().showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
// Elimina les finestres antigues
if (_statisticsWindow != null) {
_statisticsWindow.dispose();
_statisticsWindow = null;
}
//_world.clean();
ObjectInputStream inputStream;
try {
File f = getWorldChooser().getSelectedFile();
FileInputStream fileStream = new FileInputStream(f);
inputStream = new ObjectInputStream(fileStream);
_world = (World) inputStream.readObject();
inputStream.close();
_gameFile = f;
_trackedOrganism = null;
processState = true;
setStatusMessage(Messages.getString("T_WORLD_LOADED_SUCCESSFULLY")); //$NON-NLS-1$
} catch (IOException ex) {
System.err.println(ex.getMessage());
JOptionPane.showMessageDialog(null,Messages.getString("T_CANT_READ_FILE"),Messages.getString("T_READ_ERROR"),JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
} catch (ClassNotFoundException ex) {
System.err.println(ex.getMessage());
JOptionPane.showMessageDialog(null,Messages.getString("T_WRONG_FILE_TYPE"),Messages.getString("T_READ_ERROR"),JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
} catch (ClassCastException ex) {
System.err.println(ex.getMessage());
JOptionPane.showMessageDialog(null,Messages.getString("T_WRONG_FILE_VERSION"),Messages.getString("T_READ_ERROR"),JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
}
// Torna a assignar els valors dels camps no guardats a l'objecte world
_world.init(_visibleWorld);
scrollPane.setViewportView(_visibleWorld);
// Assegurem que s'ha dibuixat el m�n
_visibleWorld.repaint();
}
} catch (SecurityException ex) {
System.err.println(ex.getMessage());
JOptionPane.showMessageDialog(null,Messages.getString("T_PERMISSION_DENIED"),Messages.getString("T_PERMISSION_DENIED"),JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
}
_isProcessActive = processState;
}
}
class SaveGameAsAction extends StdAction {
private static final long serialVersionUID = 1L;
public SaveGameAsAction(String text, String icon_path, String desc) {
super(text, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
saveGameAs();
}
}
class QuitAction extends StdAction {
private static final long serialVersionUID = 1L;
public QuitAction(String text, String icon_path, String desc) {
super(text, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
quit();
}
}
public void quit() {
int save = JOptionPane.showConfirmDialog(this,Messages.getString("T_SAVE_BEFORE_QUIT"), //$NON-NLS-1$
Messages.getString("T_SAVE_WORLD"),JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE); //$NON-NLS-1$
if (save != JOptionPane.CANCEL_OPTION) {
if (save == JOptionPane.YES_OPTION) {
if (_gameFile != null)
saveObject(_world, _gameFile);
else
if (saveGameAs() == null)
return;
}
Utils.quitProgram(this);
if (serverThread != null)
serverThread.closeServer();
try {
System.exit(0);
} catch (SecurityException ex) {
dispose();
}
}
}
class StatisticsAction extends StdAction {
private static final long serialVersionUID = 1L;
public StatisticsAction(String text, String icon_path, String desc) {
super(text, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
if (_statisticsWindow != null)
_statisticsWindow.dispose();
_statisticsWindow = _world.createStatisticsWindow();
}
}
class LabAction extends StdAction {
private static final long serialVersionUID = 1L;
public LabAction(String text, String icon_path, String desc) {
super(text, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
new LabWindow(MainWindow.this);
}
}
class KillAllAction extends StdAction {
private static final long serialVersionUID = 1L;
public KillAllAction(String text, String icon_path, String desc) {
super(text, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
_world.killAll();
}
}
class DisperseAllAction extends StdAction {
private static final long serialVersionUID = 1L;
public DisperseAllAction(String text, String icon_path, String desc) {
super(text, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
_world.disperseAll();
}
}
class ParametersAction extends StdAction {
private static final long serialVersionUID = 1L;
public ParametersAction(String text, String icon_path, String desc) {
super(text, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
paramDialog();
}
}
class ManualAction extends StdAction {
private static final long serialVersionUID = 1L;
public ManualAction(String text, String icon_path, String desc) {
super(text, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
BareBonesBrowserLaunch.openURL("http://biogenesis.sourceforge.net/manual.php."+Messages.getLanguage()); //$NON-NLS-1$
}
}
class CheckLastVersionAction extends StdAction {
private static final long serialVersionUID = 1L;
public CheckLastVersionAction(String text, String icon_path, String desc) {
super(text, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
checkLastVersion();
}
}
class AboutAction extends StdAction {
private static final long serialVersionUID = 1L;
public AboutAction(String text, String icon_path, String desc) {
super(text, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
about();
}
}
class NetConfigAction extends StdAction {
private static final long serialVersionUID = 1L;
public NetConfigAction(String text, String icon_path, String desc) {
super(text, icon_path, desc);
}
public void actionPerformed(ActionEvent e) {
netConfig();
}
}
protected void paramDialog() {
new ParamDialog(this);
}
public void setTrackedOrganism(Organism org) {
_trackedOrganism = org;
abortTrackingAction.setEnabled(_trackedOrganism != null);
}
protected void netConnectionsWindow() {
new NetConnectionsWindow(this);
}
public void pauseGame() {
_isProcessActive = false;
startStopAction.toogle();
_menuStartStopGame.setIcon(null);
setStatusMessage(Messages.getString("T_GAME_PAUSED")); //$NON-NLS-1$
}
public void playGame() {
_isProcessActive = true;
startStopAction.toogle();
_menuStartStopGame.setIcon(null);
setStatusMessage(Messages.getString("T_GAME_RESUMED")); //$NON-NLS-1$
}
protected File saveGameAs() {
File savedFile = saveObjectAs(_world);
if (savedFile != null)
_gameFile = savedFile;
return savedFile;
}
protected void about() {
String aboutString = Messages.getString("T_PROGRAM_NAME")+"http://biogenesis.sourceforge.net/" //$NON-NLS-1$//$NON-NLS-2$
+Messages.getString("T_VERSION")+ //$NON-NLS-1$
Messages.getString("T_COPYRIGHT")+"joanq@users.sourceforge.net\n"+ //$NON-NLS-1$ //$NON-NLS-2$
Messages.getString("T_ARTWORK_BY")+" Ananda Daydream, Florian Haag (http://toolbaricons.sourceforge.net/)"; //$NON-NLS-1$//$NON-NLS-2$
JOptionPane.showMessageDialog(this, aboutString, Messages.getString("T_ABOUT"), //$NON-NLS-1$
JOptionPane.INFORMATION_MESSAGE,imageIcon);
}
private void setControls () {
setIconImage(imageIcon.getImage());
JPanel centralPanel = new JPanel();
centralPanel.setLayout(new BorderLayout());
_visibleWorld=new VisibleWorld(this);
scrollPane = new JScrollPane(_visibleWorld);
scrollPane.getHorizontalScrollBar().setUnitIncrement(10);
scrollPane.getVerticalScrollBar().setUnitIncrement(10);
setLocation(Utils.WINDOW_X, Utils.WINDOW_Y);
setExtendedState(Utils.WINDOW_STATE);
getContentPane().setLayout(new BorderLayout());
infoToolbar = new InfoToolbar(null, this);
centralPanel.add(scrollPane, BorderLayout.CENTER);
centralPanel.add(infoToolbar, BorderLayout.SOUTH);
getContentPane().add(centralPanel, BorderLayout.CENTER);
_statusLabel = new JLabel(" "); //$NON-NLS-1$
_statusLabel.setBorder(new EtchedBorder());
_nf = NumberFormat.getInstance();
_nf.setMaximumFractionDigits(1);
getContentPane().add(_statusLabel, BorderLayout.SOUTH);
getContentPane().add(toolBar, BorderLayout.NORTH);
worldChooser.setFileFilter(new BioFileFilter(BioFileFilter.WORLD_EXTENSION));
geneticCodeChooser.setFileFilter(new BioFileFilter(BioFileFilter.GENETIC_CODE_EXTENSION));
}
public File saveObjectAs(Object obj) {
File resultFile = null;
boolean processState = _isProcessActive;
_isProcessActive = false;
try {
JFileChooser chooser;
if (obj instanceof GeneticCode)
chooser = getGeneticCodeChooser();
else
chooser = getWorldChooser();
int returnVal = chooser.showSaveDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
int canWrite = JOptionPane.YES_OPTION;
File f = chooser.getSelectedFile();
String filename = f.getName();
String ext = (filename.lastIndexOf(".")==-1)?"":filename.substring(filename.lastIndexOf(".")+1,filename.length());
if (ext.equals("")) {
f = new File(f.getAbsolutePath()+"."+((BioFileFilter)chooser.getFileFilter()).getValidExtension());
chooser.setSelectedFile(f);
}
if (f.exists()) {
canWrite = JOptionPane.showConfirmDialog(null,Messages.getString("T_CONFIRM_FILE_OVERRIDE"), //$NON-NLS-1$
Messages.getString("T_FILE_EXISTS"),JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$
}
if (canWrite == JOptionPane.YES_OPTION) {
if (obj instanceof GeneticCode) {
try {
BioXMLParser.writeGeneticCode(new PrintStream(f),(GeneticCode)obj);
resultFile = f;
} catch (FileNotFoundException ex) {
System.err.println(ex.getLocalizedMessage());
}
} else
if (saveObject(obj, f))
resultFile = f;
}
}
} catch (SecurityException ex) {
System.err.println(ex.getMessage());
JOptionPane.showMessageDialog(null,Messages.getString("T_PERMISSION_DENIED"),Messages.getString("T_PERMISSION_DENIED"),JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
}
_isProcessActive = processState;
return resultFile;
}
public boolean saveObject(Object obj, File f) {
ObjectOutputStream outputStream;
try {
FileOutputStream fileStream = new FileOutputStream(f);
outputStream = new ObjectOutputStream(fileStream);
outputStream.writeObject(obj);
outputStream.close();
setStatusMessage(Messages.getString("T_WRITING_COMPLETED")); //$NON-NLS-1$
return true;
} catch (FileNotFoundException e) {
System.err.println(e.getMessage());
} catch (IOException e) {
System.err.println(e.getMessage());
} catch (SecurityException ex) {
System.err.println(ex.getMessage());
JOptionPane.showMessageDialog(null,Messages.getString("T_PERMISSION_DENIED"),Messages.getString("T_PERMISSION_DENIED"),JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
}
return false;
}
public void configureApp() {
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
quit();
}
});
setTitle(Messages.getString("T_BIOGENESIS")); //$NON-NLS-1$
UIManager.put("OptionPane.yesButtonText", Messages.getString("T_YES"));
UIManager.put("OptionPane.noButtonText", Messages.getString("T_NO"));
UIManager.put("OptionPane.cancelButtonText", Messages.getString("T_CANCEL"));
}
public void updateStatusLabel() {
statusLabelText.setLength(0);
statusLabelText.append(Messages.getString("T_FPS")); //$NON-NLS-1$
statusLabelText.append(getFPS());
statusLabelText.append(" "); //$NON-NLS-1$
statusLabelText.append(Messages.getString("T_TIME")); //$NON-NLS-1$
statusLabelText.append(_world.getTime());
statusLabelText.append(" "); //$NON-NLS-1$
statusLabelText.append(Messages.getString("T_CURRENT_POPULATION")); //$NON-NLS-1$
statusLabelText.append(_world.getPopulation());
statusLabelText.append(" "); //$NON-NLS-1$
statusLabelText.append(Messages.getString("T_O2")); //$NON-NLS-1$
statusLabelText.append(_nf.format(_world.getO2()));
statusLabelText.append(" "); //$NON-NLS-1$
statusLabelText.append(Messages.getString("T_CO2")); //$NON-NLS-1$
statusLabelText.append(_nf.format(_world.getCO2()));
statusLabelText.append(" "); //$NON-NLS-1$
statusLabelText.append(getStatusMessage());
_statusLabel.setText(statusLabelText.toString());
}
/**
* Returns the actual Frame Per Second rate of the program.
*
* @return The actual FPS.
*/
public int getFPS() {
return 1000/currentDelay;
}
final transient Runnable lifeProcess = new Runnable() {
public void run() {
if (_isProcessActive) {
// executa un torn
_world.time();
nFrames++;
if (nFrames % 20 == 0) {
//if (_statisticsWindow != null)
// _statisticsWindow.recalculate();
updateStatusLabel();
}
// dibuixa de nou si cal
_world.setPaintingRegion();
// tracking
if (_trackedOrganism != null) {
if (!_trackedOrganism.isAlive()) {
_trackedOrganism = null;
abortTrackingAction.setEnabled(false);
}
else {
JScrollBar bar = scrollPane.getHorizontalScrollBar();
bar.setValue(Utils.between(_trackedOrganism._centerX - scrollPane.getWidth()/2,
bar.getValue()-2*(int)Utils.MAX_VEL,bar.getValue()+2*(int)Utils.MAX_VEL));
bar = scrollPane.getVerticalScrollBar();
bar.setValue(Utils.between(_trackedOrganism._centerY - scrollPane.getHeight()/2,
bar.getValue()-2*(int)Utils.MAX_VEL,bar.getValue()+2*(int)Utils.MAX_VEL));
}
}
}
}
};
public void startApp() {
/*GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
if (gd.isFullScreenSupported()) {
setResizable(false);
setUndecorated(true);
gd.setFullScreenWindow(this);
validate();
} else {*/
setResizable(true);
setSize(new Dimension(Utils.WINDOW_WIDTH,Utils.WINDOW_HEIGHT));
setVisible(true);
//}
_timer = new java.util.Timer();
startLifeProcess(Utils.DELAY);
if (isAcceptingConnections())
startServer();
}
/**
* Number of milliseconds between two invocations to the lifeProcess invocation.
* It starts as the user's preference DELAY but is adapted to the current situation
* in the world and the machine speed.
*/
protected int currentDelay = Utils.DELAY;
/**
* If positive, the consecutive number of frames that haven't accomplished the expected time contract.
* If negative, the consecutive number of frame that have accomplished the expected time contract.
* Used to adapt the program's speed.
*/
protected int failedTime=0;
/**
* The moment when the last painting of this visual world started. Used to control
* the program's speed.
*/
protected long lastPaintTime;
public void startLifeProcess(int delay) {
if (updateTask != null)
updateTask.cancel();
updateTask = new TimerTask() {
@Override
public void run() {
try {
EventQueue.invokeAndWait(lifeProcess);
/**
* Checks the actual drawing speed and increases or decreases the speed
* of the program in order to keep it running smoothly.
*/
long actualTime = System.currentTimeMillis();
if (actualTime - lastPaintTime > currentDelay*1.5 || currentDelay < Utils.DELAY) {
// We can't run so fast
failedTime=Math.max(failedTime+1, 0);
if (failedTime >= 2) {
failedTime = 0;
currentDelay*=1.5;
startLifeProcess(currentDelay);
}
} else {
if (actualTime - lastPaintTime < currentDelay*1.2 && currentDelay > Utils.DELAY) {
// We can run faster
failedTime=Math.min(failedTime-1, 0);
if (failedTime <= -10) {
currentDelay = Math.max(Utils.DELAY, currentDelay-1);
startLifeProcess(currentDelay);
}
} else
// Normal situation: we run at the expected speed
failedTime = 0;
}
if (currentDelay > 1000) {
pauseGame();
}
lastPaintTime = actualTime;
} catch (InterruptedException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
};
_timer.schedule(updateTask, delay, delay);
}
public void changeLocale() {
UIManager.put("OptionPane.yesButtonText", Messages.getString("T_YES"));
UIManager.put("OptionPane.noButtonText", Messages.getString("T_NO"));
UIManager.put("OptionPane.cancelButtonText", Messages.getString("T_CANCEL"));
_menuGame.setText(Messages.getString("T_GAME")); //$NON-NLS-1$
_menuGame.setMnemonic(Messages.getMnemonic("T_GAME").intValue()); //$NON-NLS-1$
newGameAction.changeLocale();
startStopAction.changeLocale();
openGameAction.changeLocale();
saveGameAction.changeLocale();
saveGameAsAction.changeLocale();
quitAction.changeLocale();
_menuWorld.setText(Messages.getString("T_WORLD")); //$NON-NLS-1$
_menuWorld.setMnemonic(Messages.getMnemonic("T_WORLD").intValue()); //$NON-NLS-1$
statisticsAction.changeLocale();
increaseCO2Action.changeLocale();
decreaseCO2Action.changeLocale();
parametersAction.changeLocale();
labAction.changeLocale();
killAllAction.changeLocale();
disperseAllAction.changeLocale();
_menuHelp.setText(Messages.getString("T_HELP")); //$NON-NLS-1$
_menuHelp.setMnemonic(Messages.getMnemonic("T_HELP").intValue()); //$NON-NLS-1$
manualAction.changeLocale();
checkLastVersionAction.changeLocale();
aboutAction.changeLocale();
_menuNet.setText(Messages.getString("T_NETWORK")); //$NON-NLS-1$
_menuNet.setMnemonic(Messages.getMnemonic("T_NETWORK").intValue()); //$NON-NLS-1$
netConfigAction.changeLocale();
manageConnectionsAction.changeLocale();
setTitle(Messages.getString("T_BIOGENESIS")); //$NON-NLS-1$
createMenu();
_visibleWorld.changeLocale();
infoToolbar.changeLocale();
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.