Repository navigation
Expand file tree
/
Copy pathBalancedEcosystemGUI.java
More file actions
1695 lines (1495 loc) · 64.8 KB
/
Copy pathBalancedEcosystemGUI.java
File metadata and controls
1695 lines (1495 loc) · 64.8 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
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import java.awt.*;
import javax.swing.*;
import java.awt.event.*; // Needed for ActionListener
import javax.swing.event.*; // Needed for ActionListener
//for start screen
import javax.imageio.ImageIO;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.swing.JSlider;
import java.io.File;
class BalancedEcosystemGUI extends JFrame implements ActionListener, ChangeListener, MouseListener
{
///////////////////code for simulation/////////////////////
//set up environment
private Environment environment = new Environment (0.05,0.01,0.001);
//set up 2nd screen with user manipulable variables
protected static ConstantControl control;
private JSlider speedSldr = new JSlider ();//set up slider
private Timer t;//timer
//set up graph and scroll
private Graph graph;
private JScrollPane scroll;
//buttons
private JButton startBtn;
private JButton stopBtn;
private JButton incrementBtn;
private JButton foodBtn;
private JButton preyBtn;
private JButton predatorBtn;
private JButton togglePointerBtn;
private JButton loadBtn;
//options for different preset environments
private JComboBox biome;
//set up generation counter
protected static int generation = 0;
// set up statistic labels
private static JLabel generationCount = new JLabel("Generation: 0");
protected static JLabel foodCount = new JLabel("Food: 0");
protected static JLabel preyCount = new JLabel("Prey: 0");
static JLabel predatorCount = new JLabel("Predator: 0");
//set up label for speed slider
private JLabel speedSlider = new JLabel("Speed Slider");
///////////////////code for mouse interactions////////////////
//set up populate/eradicate rate
//drop down box of options (0% - 100%)
private JLabel density = new JLabel("Density (%): ");
private JComboBox rate;
protected static double percent = 0.5;
//set up booleans for mouse events
protected static boolean addFood = true;
protected static boolean addPrey = false;
protected static boolean addPredator = false;
private boolean pointer = false;
private boolean mousePress = false;
private boolean populate = true;
private boolean eradicate = false;
//set up mouse points
private static Point pressed;
private static Point released;
private static Point dragged;
//set up necessary points for drawing
private static int xDragged = 0;
private static int yDragged = 0;
//set up boolean so graph can stop drawing
private boolean pause = true;
//////////////////////code for start screens///////////////////////////////
//set up labels
private JLabel foodSpawnLbl, foodLimitLbl;
private JLabel foodEnergyLbl, preyHungerLbl, preyFoodRequiredLbl, preyRepCoolDownLbl;
private JLabel preyEnergyLbl, predatorHungerLbl, predatorFoodRequiredLbl, predatorRepCoolDownLbl;
//set up sliders
private static JSlider foodSpawnSl, foodLimitSl;
private static JSlider foodEnergySl, preyHungerSl, preyFoodRequiredSl, preyRepCoolDownSl;
private static JSlider preyEnergySl, predatorHungerSl, predatorFoodRequiredSl, predatorRepCoolDownSl;
//set up buttons for choosing options
JButton mouseBtn, rabbitBtn, deerBtn, snakeBtn, wolfBtn, bearBtn, defaultBtn;
//set up panels (1st screen, 2md screen, simulation screen)
private JPanel screen2 = new JPanel();
private JPanel screen1 = new JPanel();
private JPanel content = new JPanel ();
//======================================================== constructor
public BalancedEcosystemGUI ()
{
//make simulation grid and graph
DrawArea board = new DrawArea (500, 525);
graph = new Graph();
//add listeners
board.addMouseListener(this);
speedSldr.addChangeListener (this);
//set up simulation screen layout
content.setLayout (new BorderLayout ());
//create major panels
JPanel control = new JPanel();
JPanel graphPanel = new JPanel();
//make control panel
startBtn = new JButton ("Start");
stopBtn = new JButton("Stop");
incrementBtn = new JButton ("Increment");
loadBtn = new JButton("Load");
startBtn.addActionListener(this);
stopBtn.addActionListener(this);
incrementBtn.addActionListener(this);
loadBtn.addActionListener(this);
control.setLayout(new GridLayout(2,2));
control.add(startBtn);
control.add(stopBtn);
control.add(incrementBtn);
control.add(loadBtn);
//create options in drop down box for preset simulations
String [] options = {"stable","unstable","invasive species","bacterial growth","slow growth","desert", "sandbox mode"};
biome = new JComboBox(options);
//////////////////set up for mouse events///////////////////
//set up buttons --> to add food, prey, or predators
foodBtn = new JButton("Food");
foodBtn.addActionListener(new MouseControlListener());
preyBtn = new JButton("Prey");
preyBtn.addActionListener(new MouseControlListener());
predatorBtn = new JButton("Predator");
predatorBtn.addActionListener(new MouseControlListener());
foodBtn.setOpaque(true);
preyBtn.setOpaque(false);
predatorBtn.setOpaque(false);
//panel to organize mouse controls
JPanel mouseControls = new JPanel();
mouseControls.add(foodBtn);
mouseControls.add(preyBtn);
mouseControls.add(predatorBtn);
//button to toggle pointer on and off
togglePointerBtn = new JButton("Pointer Off");
togglePointerBtn.addActionListener(new MouseControlListener());
togglePointerBtn.setOpaque(false);
//fill populate/eradicate rate combobox, 0% - 100%
String[] temp = new String[11];
for(int i = 0; i<11; i++)
temp[i] = ""+i*10;
rate = new JComboBox(temp);
rate.setSelectedItem("50");
//add all mouse event items to larger pabel
JPanel mouseSettings = new JPanel();
mouseSettings.add(togglePointerBtn);
mouseSettings.add(density);
mouseSettings.add(rate);
//add everntyhing onto controlBox
JPanel controlBox = new JPanel();
controlBox.setLayout(new BoxLayout(controlBox, BoxLayout.PAGE_AXIS));
controlBox.add(Box.createRigidArea(new Dimension (0,32)));
controlBox.add(control);
controlBox.add(biome);
controlBox.add(mouseControls);
controlBox.add(mouseSettings);
togglePointerBtn.setAlignmentX(Component.CENTER_ALIGNMENT );
controlBox.setMaximumSize(new Dimension(300,10));
//setup graph
graphPanel.setLayout(new BoxLayout(graphPanel, BoxLayout.PAGE_AXIS));
graphPanel.setPreferredSize(new Dimension(250,270));
//paint blank graph and create scroll bars
graph.draw();
ImageIcon icon = new ImageIcon(graph.getPane());
scroll = new JScrollPane(new JLabel(icon));
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
//add counters and graph to larger panel
graphPanel.add(generationCount);
graphPanel.add(foodCount);
graphPanel.add(preyCount);
graphPanel.add(predatorCount);
graphPanel.add(scroll);
//set up west and east
content.setLayout(new BorderLayout());
JPanel west = new JPanel();
west.setLayout(new BoxLayout(west, BoxLayout.PAGE_AXIS));
JPanel east = new JPanel();
east.setLayout(new BoxLayout(east, BoxLayout.PAGE_AXIS));
east.setPreferredSize(new Dimension (330,500));
//add everything to content pane
west.add(speedSlider);
west.add(speedSldr);
west.add(board);
east.add(controlBox);
east.add(graphPanel);
//fix alignment of speedslider
speedSlider.setAlignmentX(Component.CENTER_ALIGNMENT);
content.add(west,"West");
content.add(east,"East");
//call methods to load start screens
setUpScreen1();
setUpScreen2();
//finish
setContentPane (screen1);
pack ();
setTitle ("Balanced Ecosystem");
setSize(850,600);
setResizable(false);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo (null);
}
public void setUpScreen1()
{
//this is the intro screen
Color customColor = new Color(0,128,255); //or (13,148,255)
screen1.setBackground(customColor);
BtnListener btnListener = new BtnListener (); // listener for all buttons
screen1.setLayout(new GridBagLayout()); //creatins the gridbagLayout
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
//button to move to next screen plus all gridBagLayout formatting
JButton startBtn = new JButton ("Start");
startBtn.addActionListener (btnListener);
startBtn.setPreferredSize (new Dimension (150,50));
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0,0,0,0);
c.gridx = 4;
c.gridy = 4;
screen1.add(startBtn,c);
//buttons for each animal in selection
//load image of population cycles
BufferedImage img = null;
try
{
img = ImageIO.read (new File ("cycle.jpg"));
}
catch (IOException e)
{
System.out.println("File not found");
}
ImageIcon imgIcon = new ImageIcon(img);
JLabel cycle = new JLabel(new ImageIcon(img));
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0,0,0,0);
c.gridx = 0;
c.gridy = 0;
screen1.add(cycle,c);
}
public void setUpScreen2()
{
//set up layout of 2nd screen
screen2.setLayout(new GridBagLayout());
screen2.setBackground(new Color(0,128,255));
screen2.setForeground(Color.black);
GridBagConstraints c = new GridBagConstraints();
//set up spawn rate slider and label
foodSpawnLbl = new JLabel("Food Spawn rate (%):");
foodSpawnLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
foodSpawnLbl.setAlignmentY(Component.CENTER_ALIGNMENT);
screen2.add(foodSpawnLbl);
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0,0,0,0);
c.gridx = 0;
c.gridy = 1;
c.gridheight = 1;
screen2.add(foodSpawnLbl,c);
foodSpawnSl = new JSlider(JSlider.HORIZONTAL, 0, 5, 0);
foodSpawnSl.addChangeListener (new SliderListener1());
foodSpawnSl.setMinorTickSpacing(1);
foodSpawnSl.setMajorTickSpacing(5);
foodSpawnSl.setPaintTicks(true);
foodSpawnSl.setPaintLabels(true);
foodSpawnSl.setPreferredSize(new Dimension(300, 80));
c.insets = new Insets(0,0,0,0);
c.gridx = 2;
c.gridy = 1;
c.gridwidth = 5;
c.weightx = 8;
screen2.add(foodSpawnSl,c);
c.gridwidth = 1;
c.weightx = 1;
//set up food limit slider and label
foodLimitLbl = new JLabel("Food Expiration Rate (# of turns):");//, GridBagLayout.NORTH);
foodLimitLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0,0,0,0);
c.gridx = 0;
c.gridy = 2;
screen2.add(foodLimitLbl,c);
foodLimitSl = new JSlider(JSlider.HORIZONTAL, 10, 30, 10);
foodLimitSl.addChangeListener (new SliderListener2());
foodLimitSl.setMinorTickSpacing(1);
foodLimitSl.setMajorTickSpacing(5);
foodLimitSl.setPaintTicks(true);
foodLimitSl.setPaintLabels(true);
foodLimitSl.setPreferredSize(new Dimension(300, 80));
c.insets = new Insets(0,0,0,0);
c.gridx = 2;
c.gridy = 2;
c.gridwidth = 3;
screen2.add(foodLimitSl,c);
c.gridwidth = 1;
//set up food energy slider and label
foodEnergyLbl = new JLabel("Food Energy:");//, GridBagLayout.NORTH);
foodEnergyLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0,0,0,0);
c.gridx = 0;
c.gridy = 3;
screen2.add(foodEnergyLbl,c);
foodEnergySl = new JSlider(JSlider.HORIZONTAL, 0, 100, 0);
foodEnergySl.addChangeListener (new SliderListener3());
foodEnergySl.setMinorTickSpacing(5);
foodEnergySl.setMajorTickSpacing(10);
foodEnergySl.setPaintTicks(true);
foodEnergySl.setPaintLabels(true);
foodEnergySl.setPreferredSize(new Dimension(300, 80));
c.insets = new Insets(0,0,0,0);
c.gridx = 2;
c.gridy = 3;
c.gridwidth = 3;
screen2.add(foodEnergySl,c);
c.gridwidth = 1;
//set up prey hunger slider and label
preyHungerLbl = new JLabel("Prey Hunger:");
preyHungerLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0,0,0,0);
c.gridx = 0;
c.gridy = 4;
screen2.add(preyHungerLbl,c);
preyHungerSl = new JSlider(JSlider.HORIZONTAL, 5, 30, 5);
preyHungerSl.addChangeListener (new SliderListener4());
preyHungerSl.setMinorTickSpacing(1);
preyHungerSl.setMajorTickSpacing(5);
preyHungerSl.setPaintTicks(true);
preyHungerSl.setPaintLabels(true);
preyHungerSl.setPreferredSize(new Dimension(300, 80));
c.insets = new Insets(0,0,0,0);
c.gridx = 2;
c.gridy = 4;
c.gridwidth = 3;
screen2.add(preyHungerSl,c);
c.gridwidth = 1;
//set up prey food required slider and label
preyFoodRequiredLbl = new JLabel("Food Required for Prey Reproduction:");
preyFoodRequiredLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0,0,0,0);
c.gridx = 0;
c.gridy = 5;
screen2.add(preyFoodRequiredLbl,c);
preyFoodRequiredSl = new JSlider(JSlider.HORIZONTAL, 0,30, 0);
preyFoodRequiredSl.addChangeListener (new SliderListener5());
preyFoodRequiredSl.setMinorTickSpacing(1);
preyFoodRequiredSl.setMajorTickSpacing(5);
preyFoodRequiredSl.setPaintTicks(true);
preyFoodRequiredSl.setPaintLabels(true);
preyFoodRequiredSl.setPreferredSize(new Dimension(300, 80));
c.insets = new Insets(0,0,0,0);
c.gridx = 2;
c.gridy = 5;
c.gridwidth = 3;
screen2.add(preyFoodRequiredSl,c);
c.gridwidth = 1;
//set up prey reproduction slider and label
preyRepCoolDownLbl = new JLabel("Prey Reproduction Period:");
preyRepCoolDownLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0,0,0,0);
c.gridx =0;
c.gridy = 6;
screen2.add(preyRepCoolDownLbl,c);
preyRepCoolDownSl = new JSlider(JSlider.HORIZONTAL, 0, 30, 0);
preyRepCoolDownSl.addChangeListener (new SliderListener6());
preyRepCoolDownSl.setMinorTickSpacing(1);
preyRepCoolDownSl.setMajorTickSpacing(5);
preyRepCoolDownSl.setPaintTicks(true);
preyRepCoolDownSl.setPaintLabels(true);
preyRepCoolDownSl.setPreferredSize(new Dimension(300, 80));
c.insets = new Insets(0,0,0,0);
c.gridx = 2;
c.gridy = 6;
c.gridwidth = 3;
screen2.add(preyRepCoolDownSl,c);
c.gridwidth = 1;
//set up prey energy slider and label
preyEnergyLbl = new JLabel("Prey Energy:");
preyEnergyLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0,0,0,0);
c.gridx = 0;
c.gridy = 7;
screen2.add(preyEnergyLbl,c);
preyEnergySl = new JSlider(JSlider.HORIZONTAL, 0, 30, 0);
preyEnergySl.addChangeListener (new SliderListener7());
preyEnergySl.setMinorTickSpacing(1);
preyEnergySl.setMajorTickSpacing(5);
preyEnergySl.setPaintTicks(true);
preyEnergySl.setPaintLabels(true);
preyEnergySl.setPreferredSize(new Dimension(300, 80));
c.insets = new Insets(0,0,0,0);
c.gridx = 2;
c.gridy = 7;
c.gridwidth = 3;
screen2.add(preyEnergySl,c);
c.gridwidth = 1;
//set up predator hunger slider and label
predatorHungerLbl = new JLabel("Predator Hunger:");
predatorHungerLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0,0,0,0);
c.gridx = 0;
c.gridy = 8;
screen2.add(predatorHungerLbl,c);
predatorHungerSl = new JSlider(JSlider.HORIZONTAL, 5, 50, 5);
predatorHungerSl.addChangeListener (new SliderListener8());
predatorHungerSl.setMinorTickSpacing(5);
predatorHungerSl.setMajorTickSpacing(10);
predatorHungerSl.setPaintTicks(true);
predatorHungerSl.setPaintLabels(true);
predatorHungerSl.setPreferredSize(new Dimension(300, 80));
c.insets = new Insets(0,0,0,0);
c.gridx = 2;
c.gridy = 8;
c.gridwidth = 3;
screen2.add(predatorHungerSl,c);
c.gridwidth = 1;
//set up predator food required slider and label
predatorFoodRequiredLbl = new JLabel("Food Required for Predator Reproduction:");
predatorFoodRequiredLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0,0,0,0);
c.gridx = 0;
c.gridy = 9;
screen2.add(predatorFoodRequiredLbl,c);
predatorFoodRequiredSl = new JSlider(JSlider.HORIZONTAL, 0, 50, 0);
predatorFoodRequiredSl.addChangeListener (new SliderListener9());
predatorFoodRequiredSl.setMinorTickSpacing(5);
predatorFoodRequiredSl.setMajorTickSpacing(10);
predatorFoodRequiredSl.setPaintTicks(true);
predatorFoodRequiredSl.setPaintLabels(true);
predatorFoodRequiredSl.setPreferredSize(new Dimension(300, 80));
c.insets = new Insets(0,0,0,0);
c.gridx = 2;
c.gridy = 9;
c.gridwidth = 3;
screen2.add(predatorFoodRequiredSl,c);
c.gridwidth = 1;
//set up predator reproduction slider and label
predatorRepCoolDownLbl = new JLabel("Predator Reproduction Period:");
predatorRepCoolDownLbl.setAlignmentX(Component.CENTER_ALIGNMENT);
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0,0,0,0);
c.gridx = 0;
c.gridy = 10;
screen2.add(predatorRepCoolDownLbl,c);
predatorRepCoolDownSl = new JSlider(JSlider.HORIZONTAL, 0, 50, 0);
predatorRepCoolDownSl.addChangeListener (new SliderListener10());
predatorRepCoolDownSl.setMinorTickSpacing(5);
predatorRepCoolDownSl.setMajorTickSpacing(10);
predatorRepCoolDownSl.setPaintTicks(true);
predatorRepCoolDownSl.setPaintLabels(true);
predatorRepCoolDownSl.setPreferredSize(new Dimension(300, 80));
c.insets = new Insets(0,0,0,0);
c.gridx = 2;
c.gridy = 10;
c.gridwidth = 3;
screen2.add(predatorRepCoolDownSl,c);
c.gridwidth = 1;
//set initial values of all sliders to default values
foodSpawnSl.setValue(1);
foodLimitSl.setValue(20);
foodEnergySl.setValue(4);
preyHungerSl.setValue(15);
preyFoodRequiredSl.setValue(15);
preyRepCoolDownSl.setValue(5);
preyEnergySl.setValue(5);
predatorHungerSl.setValue(30);
predatorFoodRequiredSl.setValue(100);
predatorRepCoolDownSl.setValue(5);
//set text of all labels to default values (same as sliders)
foodSpawnLbl.setText("Food Spawn Rate (%): " + foodSpawnSl.getValue());
foodLimitLbl.setText("Food Expiration Rate ( # of turns): " + foodLimitSl.getValue());
foodEnergyLbl.setText("Food Energy: " + foodEnergySl.getValue());
preyHungerLbl.setText("Prey Hunger: " + preyHungerSl.getValue());
preyFoodRequiredLbl.setText("Food Required for Prey Reproduction: " + preyFoodRequiredSl.getValue());
preyRepCoolDownLbl.setText("Prey Reproduction Rate: " + preyRepCoolDownSl.getValue());
preyEnergyLbl.setText("Prey Energy: " + preyEnergySl.getValue());
predatorHungerLbl.setText("Predator Hunger: " + predatorHungerSl.getValue());
predatorFoodRequiredLbl.setText("Food Required for Predator Reproduction: " + predatorFoodRequiredSl.getValue());
predatorRepCoolDownLbl.setText("Predator Reproduction Rate: " + predatorRepCoolDownSl.getValue());
//color labels
foodSpawnLbl.setForeground(Color.green);
foodLimitLbl.setForeground(Color.green);
foodEnergyLbl.setForeground(new Color(0,191,255));
preyHungerLbl.setForeground(new Color(0,191,255));
preyFoodRequiredLbl.setForeground(new Color(0,191,255));
preyRepCoolDownLbl.setForeground(new Color(0,191,255));
preyEnergyLbl.setForeground(Color.red);
predatorHungerLbl.setForeground(Color.red);
predatorFoodRequiredLbl.setForeground(Color.red);
predatorRepCoolDownLbl.setForeground(Color.red);
//add go button at the bottom of the screen
//the go button to progess to the next screen
JButton goBtn = new JButton ("GO!");
goBtn.setBackground(Color.orange);
goBtn.setOpaque(true);
BtnListener btnListener = new BtnListener (); // listener for all buttons
goBtn.addActionListener(btnListener);
//set up panel with preset options for prey and predator
JPanel options = new JPanel();
options.setLayout(new GridLayout(2,3));
//creat buttons
mouseBtn = new JButton("Mouse");
rabbitBtn = new JButton("Rabbit");
deerBtn = new JButton("Deer");
snakeBtn = new JButton("Snake");
wolfBtn = new JButton("Wolf");
bearBtn = new JButton("Bear");
defaultBtn = new JButton("Default");
//add listeners
mouseBtn.addActionListener(new BtnListener());
rabbitBtn.addActionListener(new BtnListener());
deerBtn.addActionListener(new BtnListener());
snakeBtn.addActionListener(new BtnListener());
wolfBtn.addActionListener(new BtnListener());
bearBtn.addActionListener(new BtnListener());
defaultBtn.addActionListener(new BtnListener());
//color the buttons
mouseBtn.setBackground(new Color(0,191,255));
mouseBtn.setOpaque(true);
rabbitBtn.setBackground(new Color(0,191,255));
rabbitBtn.setOpaque(true);
deerBtn.setBackground(new Color(0,191,255));
deerBtn.setOpaque(true);
snakeBtn.setBackground(Color.red);
snakeBtn.setOpaque(true);
wolfBtn.setBackground(Color.red);
wolfBtn.setOpaque(true);
bearBtn.setBackground(Color.red);
bearBtn.setOpaque(true);
//finish
options.add(mouseBtn);
options.add(rabbitBtn);
options.add(deerBtn);
options.add(snakeBtn);
options.add(wolfBtn);
options.add(bearBtn);
//panel for defaultBtn and goBtn
JPanel other = new JPanel();
other.setLayout(new BoxLayout(other, BoxLayout.LINE_AXIS));
other.add(defaultBtn);
other.add(Box.createRigidArea(new Dimension(20,0)));
other.add(goBtn);
other.setOpaque(false);
JPanel total = new JPanel();
total.setLayout(new BoxLayout(total, BoxLayout.PAGE_AXIS));
total.add(options);
total.add(other);
total.setOpaque(false);
//add options and default btn to screen2
screen2.add(total);
}
class BtnListener implements ActionListener // Button menu
{
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand ().equals ("Start"))
{
screen1.removeAll();
// removes the first panel, adds the second, then refreshes so user can progress.
screen1.setVisible(false);
screen2.setVisible(true);
setContentPane(screen2);
//replace content pane with 2nd screen
}
else if (e.getActionCommand ().equals ("GO!"))
{
//replaces content pane with simulation screen
screen2.setVisible(false);
content.setVisible(true);
setContentPane(content);
content.updateUI();
//set food variables
Food.setFoodSpawn(foodSpawnSl.getValue()/100.0);
Food.setFoodLimit((int)foodLimitSl.getValue());
//set prey variables
Prey.setFoodEnergy((int)foodEnergySl.getValue());
Prey.setHunger((int)preyHungerSl.getValue());
Prey.setFoodRequired((int)preyFoodRequiredSl.getValue());
Prey.setRepCoolDown((int)preyRepCoolDownSl.getValue());
//set predator variables
Predator.setPreyEnergy((int)preyEnergySl.getValue());
Predator.setHunger((int)predatorHungerSl.getValue());
Predator.setFoodRequired((int)predatorFoodRequiredSl.getValue());
Predator.setRepCoolDown((int)predatorRepCoolDownSl.getValue());
//make new window for variable controls
control = new ConstantControl();
control.setVisible(true);
}
else if(e.getSource() == mouseBtn)//
{
//change values on slider to that of mouse
foodEnergySl.setValue(4);
preyHungerSl.setValue(20);
preyFoodRequiredSl.setValue(5);
preyRepCoolDownSl.setValue(5);
//change values on labels to match slider
foodEnergyLbl.setText("Food Energy: " + foodEnergySl.getValue());
preyHungerLbl.setText("Prey Hunger: " + preyHungerSl.getValue());
preyFoodRequiredLbl.setText("Food Required for Prey Reproduction: " + preyFoodRequiredSl.getValue());
preyRepCoolDownLbl.setText("Prey Reproduction Period: " + preyRepCoolDownSl.getValue());
}
else if(e.getSource() == rabbitBtn)
{
//change values on slider to that of rabbit
foodEnergySl.setValue(3);
preyHungerSl.setValue(30);
preyFoodRequiredSl.setValue(15);
preyRepCoolDownSl.setValue(5);
//change values on labels to match slider
foodEnergyLbl.setText("Food Energy: " + foodEnergySl.getValue());
preyHungerLbl.setText("Prey Hunger: " + preyHungerSl.getValue());
preyFoodRequiredLbl.setText("Food Required for Prey Reproduction: " + preyFoodRequiredSl.getValue());
preyRepCoolDownLbl.setText("Prey Reproduction Period: " + preyRepCoolDownSl.getValue());
}
else if(e.getSource() == deerBtn)
{
foodEnergySl.setValue(7);
preyHungerSl.setValue(20);
preyFoodRequiredSl.setValue(10);
preyRepCoolDownSl.setValue(15);
foodEnergyLbl.setText("Food Energy: " + foodEnergySl.getValue());
preyHungerLbl.setText("Prey Hunger: " + preyHungerSl.getValue());
preyFoodRequiredLbl.setText("Food Required for Prey Reproduction: " + preyFoodRequiredSl.getValue());
preyRepCoolDownLbl.setText("Prey Reproduction Period: " + preyRepCoolDownSl.getValue());
}
else if(e.getSource() == snakeBtn)
{
preyEnergySl.setValue(100);
predatorHungerSl.setValue(20);
predatorFoodRequiredSl.setValue(500);
predatorRepCoolDownSl.setValue(500);
preyEnergyLbl.setText("Prey Energy: " + preyEnergySl.getValue());
predatorHungerLbl.setText("Predator Hunger: " + predatorHungerSl.getValue());
predatorFoodRequiredLbl.setText("Food Required for Predator Reproduction: " + predatorFoodRequiredSl.getValue());
predatorRepCoolDownLbl.setText("Predator Reproduction Period: " + predatorRepCoolDownSl.getValue());
}
else if(e.getSource() == wolfBtn)
{
preyEnergySl.setValue(5);
predatorHungerSl.setValue(40);
predatorFoodRequiredSl.setValue(50);
predatorRepCoolDownSl.setValue(40);
preyEnergyLbl.setText("Prey Energy: " + preyEnergySl.getValue());
predatorHungerLbl.setText("Predator Hunger: " + predatorHungerSl.getValue());
predatorFoodRequiredLbl.setText("Food Required for Predator Reproduction: " + predatorFoodRequiredSl.getValue());
predatorRepCoolDownLbl.setText("Predator Reproduction Period: " + predatorRepCoolDownSl.getValue());
}
else if(e.getSource() == bearBtn)
{
preyEnergySl.setValue(5);
predatorHungerSl.setValue(100);
predatorFoodRequiredSl.setValue(100);
predatorRepCoolDownSl.setValue(100);
preyEnergyLbl.setText("Prey Energy: " + preyEnergySl.getValue());
predatorHungerLbl.setText("Predator Hunger: " + predatorHungerSl.getValue());
predatorFoodRequiredLbl.setText("Food Required for Predator Reproduction: " + predatorFoodRequiredSl.getValue());
predatorRepCoolDownLbl.setText("Predator Reproduction Period: " + predatorRepCoolDownSl.getValue());
}
else if(e.getSource() == defaultBtn)
{
//change all slider values to default values
foodSpawnSl.setValue(1);
foodLimitSl.setValue(20);
foodEnergySl.setValue(4);
preyHungerSl.setValue(15);
preyFoodRequiredSl.setValue(15);
preyRepCoolDownSl.setValue(5);
preyEnergySl.setValue(5);
predatorHungerSl.setValue(30);
predatorFoodRequiredSl.setValue(100);
predatorRepCoolDownSl.setValue(5);
//changle all labels to match slider values
foodSpawnLbl.setText("Food Spawn Rate (%): " + foodSpawnSl.getValue());
foodLimitLbl.setText("Food Expiration Rate ( # of turns): " + foodLimitSl.getValue());
foodEnergyLbl.setText("Food Energy: " + foodEnergySl.getValue());
preyHungerLbl.setText("Prey Hunger: " + preyHungerSl.getValue());
preyFoodRequiredLbl.setText("Food Required for Prey Reproduction: " + preyFoodRequiredSl.getValue());
preyRepCoolDownLbl.setText("Prey Reproduction Rate: " + preyRepCoolDownSl.getValue());
preyEnergyLbl.setText("Prey Energy: " + preyEnergySl.getValue());
predatorHungerLbl.setText("Predator Hunger: " + predatorHungerSl.getValue());
predatorFoodRequiredLbl.setText("Food Required for Predator Reproduction: " + predatorFoodRequiredSl.getValue());
predatorRepCoolDownLbl.setText("Predator Reproduction Rate: " + predatorRepCoolDownSl.getValue());
}
}
}
//slider that controls food spawn
class SliderListener1 implements ChangeListener{
public void stateChanged(ChangeEvent ce)
{
JSlider foodSpawnSl = (JSlider)ce.getSource();//declare slider that is source
if(!foodSpawnSl.getValueIsAdjusting()) //if the value has changed
{
Food.setFoodSpawn(foodSpawnSl.getValue()/100.0);//changes value in Food class
foodSpawnLbl.setText("Food Spawn Rate (%): " + foodSpawnSl.getValue());//update label
}
}
}
//food limit
class SliderListener2 implements ChangeListener{
public void stateChanged(ChangeEvent ce)
{
JSlider foodLimitSl = (JSlider)ce.getSource();
if(!foodLimitSl.getValueIsAdjusting()) //if the value has changed
{
Food.setFoodLimit((int)foodLimitSl.getValue()); //
foodLimitLbl.setText("Food Expiration Rate ( # of turns): " + foodLimitSl.getValue());
}
}
}
//food energy
class SliderListener3 implements ChangeListener{
public void stateChanged(ChangeEvent ce)
{
JSlider foodEnergySl = (JSlider)ce.getSource();
if(!foodEnergySl.getValueIsAdjusting()) //if the value has changed
{
Prey.setFoodEnergy((int)foodEnergySl.getValue()); //
foodEnergyLbl.setText("Food Energy: " + foodEnergySl.getValue());
}
}
}
//prey hunger
class SliderListener4 implements ChangeListener{
public void stateChanged(ChangeEvent ce)
{
JSlider preyHungerSl = (JSlider)ce.getSource();
if(!preyHungerSl.getValueIsAdjusting()) //if the value has changed
{
Prey.setHunger( (int)preyHungerSl.getValue()); //
preyHungerLbl.setText("Prey Hunger: " + preyHungerSl.getValue());
}
}
}
// prey food required
class SliderListener5 implements ChangeListener{
public void stateChanged(ChangeEvent ce)
{
JSlider preyFoodRequiredSl = (JSlider)ce.getSource();
if(!preyFoodRequiredSl.getValueIsAdjusting()) //if the value has changed
{
Prey.setFoodRequired((int)preyFoodRequiredSl.getValue()); //
preyFoodRequiredLbl.setText("Food Required for Prey Reproduction: " + preyFoodRequiredSl.getValue());
}
}
}
//prey reproduction
class SliderListener6 implements ChangeListener{
public void stateChanged(ChangeEvent ce)
{
JSlider preyRepCoolDownSl = (JSlider)ce.getSource();
if(!preyRepCoolDownSl.getValueIsAdjusting()) //if the value has changed
{
Prey.setRepCoolDown((int)preyRepCoolDownSl.getValue()); //
preyRepCoolDownLbl.setText("Prey Reproduction Period: " + preyRepCoolDownSl.getValue());
}
}
}
//prey energy
class SliderListener7 implements ChangeListener{
public void stateChanged(ChangeEvent ce)
{
JSlider preyEnergySl = (JSlider)ce.getSource();
if(!preyEnergySl.getValueIsAdjusting()) //if the value has changed
{
Predator.setPreyEnergy((int)preyEnergySl.getValue()); //
preyEnergyLbl.setText("Prey Energy: " + preyEnergySl.getValue());
}
}
}
//predator hunger
class SliderListener8 implements ChangeListener{
public void stateChanged(ChangeEvent ce)
{
JSlider predatorHungerSl = (JSlider)ce.getSource();
if(!predatorHungerSl.getValueIsAdjusting()) //if the value has changed
{
Predator.setHunger((int)predatorHungerSl.getValue()); //
predatorHungerLbl.setText("Predator Hunger: " + predatorHungerSl.getValue());
}
}
}
//predator food required
class SliderListener9 implements ChangeListener{
public void stateChanged(ChangeEvent ce)
{
JSlider predatorFoodRequiredSl = (JSlider)ce.getSource();
if(!predatorFoodRequiredSl.getValueIsAdjusting()) //if the value has changed
{
Predator.setFoodRequired((int)predatorFoodRequiredSl.getValue()); //
predatorFoodRequiredLbl.setText("Food Required for Predator Reproduction: " + predatorFoodRequiredSl.getValue());
}
}
}
//predator reproduction
class SliderListener10 implements ChangeListener{
public void stateChanged(ChangeEvent ce)
{
JSlider predatorRepCoolDownSl = (JSlider)ce.getSource();
if(!predatorRepCoolDownSl.getValueIsAdjusting()) //if the value has changed
{
Predator.setRepCoolDown((int)predatorRepCoolDownSl.getValue()); //
predatorRepCoolDownLbl.setText("Predator Reproduction Period: " + predatorRepCoolDownSl.getValue());
}
}
}
////////////////end of code for start screens/////////////////////////
/////////////////code for simulation///////////////
public void stateChanged (ChangeEvent e)//speed control
{
if (t != null)
t.setDelay (400 - 4 * speedSldr.getValue ()); // 0 to 400 ms
}
//buttons in controlBox
public void actionPerformed (ActionEvent e)
{
if (e.getSource() == startBtn)
{
//start timer
if(pause) // check if time is stopped
{
Movement moveEnvironment = new Movement (environment); // ActionListener
t = new Timer (400 - 4 * speedSldr.getValue (), moveEnvironment); // set up timer
t.start (); // start simulation
pause = false;
}
}
else if(e.getSource() == stopBtn)
{
//stop timer
try
{
t.stop();
}
catch(NullPointerException ee)
{}
pause = true;
}
else if(e.getSource() == incrementBtn)
{
//stop timer
pause = true;
try
{
t.stop();
}
catch(NullPointerException ee)
{}
//update grid, stats, and graph only 1x
environment.advance();
updateStats();
graph.draw();
}
else if(e.getSource() == loadBtn)
{
//stop timer
try
{
t.stop();
}
catch(NullPointerException ee)
{}
pause = true;
//call method in database to load proper preset sim
if(biome.getSelectedItem().equals("stable"))
Database.stable();
else if(biome.getSelectedItem().equals("unstable"))
Database.unstable();
else if(biome.getSelectedItem().equals("invasive species"))
Database.invasive();
else if(biome.getSelectedItem().equals("bacterial growth"))
Database.bacteria();
else if(biome.getSelectedItem().equals("slow growth"))
Database.slowGrowth();
else if(biome.getSelectedItem().equals("desert"))
Database.desert();
else if(biome.getSelectedItem().equals("sandbox mode"))
Database.sandbox();
//redraw the graph, clears previous data
graph.draw();