-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainFrame.java
116 lines (100 loc) · 2.73 KB
/
MainFrame.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
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.EmptyBorder;
/**
* The main screen of the program.
*
* @author Alex Pieczynski
*/
public class MainFrame extends JFrame
{
private boolean _gameStarted = false;
private CellGrid _cellGrid;
private Timer _timer;
private int _daysElapsed;
public static enum Speed
{
Slow(1000), Medium(450), Fast(150);
private int delay;
private Speed(int delay)
{
this.delay = delay;
}
}
/**
* Assembles all necessary parts of the user interface
*/
public MainFrame()
{
super("Conway's Game of Life");
EmptyBorder padding = new EmptyBorder(5,5,5,5);
_cellGrid = new CellGrid();
_cellGrid.setBorder(padding);
this.add(_cellGrid, BorderLayout.CENTER);
GridLayout layout = new GridLayout();
layout.setHgap(15);
JPanel settingsPanel = new JPanel(layout);
settingsPanel.setBorder(padding);
_daysElapsed = 0;
JLabel daysLabel = new JLabel("Days elapsed: " + _daysElapsed + " ");
settingsPanel.add(daysLabel, BorderLayout.PAGE_START);
_timer = new Timer(0, e ->
{
_cellGrid.dayPassed();
_daysElapsed++;
daysLabel.setText("Days elapsed: " + _daysElapsed);
});
JComboBox<Speed> dropdown = new JComboBox<>(Speed.values());
dropdown.setSelectedIndex(1);
settingsPanel.add(dropdown);
JButton startButton = new JButton("Start");
startButton.addActionListener( e ->
{
if (!_gameStarted)
{
_gameStarted = true;
_cellGrid.onGameStart();
Speed speed = (Speed) dropdown.getSelectedItem();
_timer.setDelay(speed.delay);
_timer.start();
}
else // game is paused
_timer.start();
startButton.setEnabled(false);
});
settingsPanel.add(startButton);
JButton pauseButton = new JButton("Pause");
pauseButton.addActionListener( e ->
{
_timer.stop();
startButton.setEnabled(true);
});
settingsPanel.add(pauseButton);
JButton resetButton = new JButton("Reset");
resetButton.addActionListener( e ->
{
_timer.stop();
_daysElapsed = 0;
daysLabel.setText("Days Elapsed: "+_daysElapsed+" ");
_cellGrid.reset();
_gameStarted = false;
startButton.setEnabled(true);
});
settingsPanel.add(resetButton);
this.add(settingsPanel, BorderLayout.PAGE_END);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.pack();
this.setLocationRelativeTo(null);
this.setResizable(false);
}
public static void main(String[] args)
{
new MainFrame().setVisible(true);
}
}