-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameOfLife.java
More file actions
329 lines (281 loc) · 10.1 KB
/
Copy pathGameOfLife.java
File metadata and controls
329 lines (281 loc) · 10.1 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
package com.company;
import javax.swing.*;
import java.awt.*;
import java.util.Objects;
/*
* This class handles the game state and the GUI displayed to the user
*/
public class GameOfLife extends JFrame {
private World world;
private boolean paused;
private boolean reset;
private JLabel generationLabel;
private JLabel aliveLabel;
private JPanel worldPanel;
private JSlider speedSlider;
private JToggleButton pauseToggleButton;
private JTextField worldSizeField;
private String mode;
private JCheckBox heatMapSelector;
private JCheckBox customColorSelector;
private JComboBox<String> colorSelector;
private JComboBox<String> backgroundColorSelector;
/*
* Set up the GUI
*/
public GameOfLife() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1000, 900);
setLayout(new BorderLayout());
//this.paused = false;
this.reset = false;
this.mode = "Default";
initComponents();
pauseToggleButton.setSelected(true);
paused = true;
setVisible(true);
}
/*
* Creates two primary sections: one for the world, and one for the controls
*/
private void initComponents() {
add(initControlPanel(), BorderLayout.WEST);
add(initWorldBox());
}
/*
* Initializes the control panel in two parts: one for info, one for buttons
*/
private JPanel initControlPanel() {
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new BorderLayout());
controlPanel.add(initControlButtons(), BorderLayout.NORTH);
controlPanel.add(initInfoBox());
return controlPanel;
}
private JPanel initControlButtons() {
JPanel buttonPanel = new JPanel();
pauseToggleButton = new JToggleButton("Pause");
pauseToggleButton.addActionListener(e -> paused = pauseToggleButton.isSelected());
JButton resetButton = new JButton("Reset");
resetButton.addActionListener(e -> {
pauseToggleButton.setSelected(true);
paused = true;
reset = true;
});
buttonPanel.add(pauseToggleButton);
buttonPanel.add(resetButton);
return buttonPanel;
}
private JPanel initInfoBox() {
JPanel infoPanel = new JPanel();
infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.Y_AXIS));
generationLabel = new JLabel("Generation #0");
aliveLabel = new JLabel("Alive: 0");
speedSlider = new JSlider(1, 10, 5);
infoPanel.add(Box.createRigidArea(new Dimension(0, 20)));
infoPanel.add(generationLabel);
infoPanel.add(aliveLabel);
infoPanel.add(Box.createRigidArea(new Dimension(0, 20)));
infoPanel.add(new JLabel("Speed Mode:"));
infoPanel.add(speedSlider);
infoPanel.add(Box.createRigidArea(new Dimension(0, 20)));
infoPanel.add(initWorldSizePanel());
infoPanel.add(Box.createRigidArea(new Dimension(0, 20)));
infoPanel.add(initColorOptions());
return infoPanel;
}
private JPanel initWorldSizePanel() {
JPanel worldSizePanel = new JPanel();
worldSizePanel.setMaximumSize(new Dimension(300, 40));
worldSizeField = new JTextField(3);
worldSizePanel.add(new JLabel("World Size:"));
worldSizePanel.add(worldSizeField);
return worldSizePanel;
}
/*
* Initialize the color options panel, which handles things like color mode selection and custom color selection
*/
private JPanel initColorOptions() {
JPanel colorOptionsPanel = new JPanel();
colorOptionsPanel.setLayout(new BoxLayout(colorOptionsPanel, BoxLayout.Y_AXIS));
heatMapSelector = new JCheckBox("Heatmap");
heatMapSelector.addActionListener(e -> {
mode = heatMapSelector.isSelected() ? "Heatmap" : "Default";
customColorSelector.setSelected(false);
displayWorld(world);
});
customColorSelector = new JCheckBox("Custom Color");
customColorSelector.addActionListener(e -> {
mode = customColorSelector.isSelected() ? "Custom" : "Default";
heatMapSelector.setSelected(false);
displayWorld(world);
});
String[] colors = {
"Dark Gray", "Black", "Blue", "Cyan", "Gray", "Light Gray", "Green", "Magenta", "Orange", "Pink", "Red", "Yellow", "White"
};
colorSelector = new JComboBox<>(colors);
colorSelector.setMaximumSize(new Dimension(250, 20));
colorSelector.addActionListener(e -> {
if (customColorSelector.isSelected()) {
displayWorld(world);
}
});
backgroundColorSelector = new JComboBox<>(colors);
backgroundColorSelector.setMaximumSize(new Dimension(250, 20));
backgroundColorSelector.setSelectedItem("White");
backgroundColorSelector.addActionListener(e -> displayWorld(world));
colorOptionsPanel.add(new JLabel("Color Options:"));
colorOptionsPanel.add(Box.createRigidArea(new Dimension(0, 10)));
colorOptionsPanel.add(heatMapSelector);
colorOptionsPanel.add(customColorSelector);
colorOptionsPanel.add(new JLabel("Foreground Color"));
colorOptionsPanel.add(colorSelector);
colorOptionsPanel.add(new JLabel("Background Color"));
colorOptionsPanel.add(backgroundColorSelector);
return colorOptionsPanel;
}
/*
* Initializes the section of the GUI where the world will be represented
*/
private JPanel initWorldBox() {
worldPanel = new JPanel();
worldPanel.setLayout(new GridLayout(10, 10, 1, 1));
worldPanel.setBackground(Color.BLACK);
return worldPanel;
}
public void displayWorld(World world) {
this.world = world;
worldPanel.removeAll();
char[][] charArray = world.getCharArray();
for (int i = 0; i < charArray.length; i++) {
for (int j = 0; j < charArray[i].length; j++) {
JPanel panel = new JPanel();
Color color;
char ch = charArray[i][j];
switch (mode) {
case "Custom":
color = selectColorCustom(ch);
break;
case "Heatmap":
int neighbors = world.getNeighborArray()[i][j];
color = SelectColorHeatmap(ch == 'O' ? neighbors + 1 : neighbors);
break;
default:
color = selectColorDefault(ch);
break;
}
panel.setBackground(color);
worldPanel.add(panel);
}
}
generationLabel.setText("Generation #" + world.getGeneration());
aliveLabel.setText("Alive: " + world.getAlive());
worldPanel.revalidate();
worldPanel.repaint();
if (world.getAlive() <= 0) {
pauseToggleButton.doClick();
}
}
private Color selectColorDefault(char ch) {
return ch == 'O' ? Color.DARK_GRAY : Color.WHITE;
}
private Color selectColorCustom(char ch) {
Color color = Color.WHITE;
//if (ch == 'O') {
switch (ch == 'O' ? Objects.requireNonNull(colorSelector.getSelectedItem()).toString()
: Objects.requireNonNull(backgroundColorSelector.getSelectedItem()).toString()) {
case "Blue":
color = Color.BLUE;
break;
case "Cyan":
color = Color.CYAN;
break;
case "Black":
color = Color.BLACK;
break;
case "Gray":
color = Color.GRAY;
break;
case "Dark Gray":
color = Color.DARK_GRAY;
break;
case "Light Gray":
color = Color.LIGHT_GRAY;
break;
case "Green":
color = Color.GREEN;
break;
case "Magenta":
color = Color.MAGENTA;
break;
case "Orange":
color = Color.ORANGE;
break;
case "Pink":
color = Color.PINK;
break;
case "Red":
color = Color.RED;
break;
case "Yellow":
color = Color.YELLOW;
break;
case "White":
color = Color.WHITE;
break;
}
//}
return color;
}
/*
* Heatmap color mode selects different colors based on the number of neighbors in an area
*/
private Color SelectColorHeatmap(int neighbors) {
Color color;
switch (neighbors) {
case 0:
color = Color.WHITE;
break;
case 1:
color = Color.BLUE;
break;
case 2:
color = Color.GREEN;
break;
case 3:
color = Color.YELLOW;
break;
case 4:
color = Color.ORANGE;
break;
default:
color = Color.RED;
break;
}
return color;
}
public void setWorldSize(int worldSize) {
worldPanel.setLayout(new GridLayout(worldSize, worldSize, 1, 1));
}
public boolean isPaused() {
return paused;
}
public boolean isReset() {
return reset;
}
public void setReset(boolean reset) {
this.reset = reset;
}
public int getSpeed() {
return speedSlider.getValue();
}
public int getWorldSize() {
if (!worldSizeField.getText().isBlank() && worldSizeField.getText().matches("\\d*")) {
int worldSize = Integer.parseInt(worldSizeField.getText());
if (worldSize > 0) {
return worldSize;
}
}
return 10;
}
}