-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUILife.java
More file actions
231 lines (212 loc) · 6.54 KB
/
Copy pathGUILife.java
File metadata and controls
231 lines (212 loc) · 6.54 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
package uk.ac.cam.efxlb2.oop.tick5;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
public class GUILife extends JFrame {
private World world;
private PatternStore store;
private ArrayList<World> cachedWorlds;
protected GamePanel gamePanel;
private JButton playButton;
private java.util.Timer timer = new java.util.Timer(true);//CHANGE
private boolean playing;
public GUILife(PatternStore ps) throws PatternFormatException{
super("Game of Life");
try {
store=ps;
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(1024,768);
add(createPatternsPanel(),BorderLayout.WEST);
add(createControlPanel(),BorderLayout.SOUTH);
gamePanel = createGamePanel();//DIFFERENT THAN THE OTHER PANELS BECAUSE HAS ITS OWN STATE
ArrayWorld world2 = new ArrayWorld(ps.getPatternsNameSorted().get(10));//TESTER
add(gamePanel,BorderLayout.CENTER);
}
catch (PatternFormatException e) {
System.out.print("Another exception...");
}
}
private void addBorder(JComponent component, String title) {
Border etch = BorderFactory.createEtchedBorder(EtchedBorder.LOWERED);
Border tb = BorderFactory.createTitledBorder(etch,title);
component.setBorder(tb);
}
private GamePanel createGamePanel() {
GamePanel gamePanel = new GamePanel();
addBorder(gamePanel,"Game Panel");
return gamePanel;
}
private Pattern[] makearray(java.util.List<Pattern> input) {
Pattern[] output = new Pattern[input.size()];
for (int i=0;i<input.size();i++) {
output[i]=input.get(i);
}
return output;
}
private JPanel createPatternsPanel() {
JPanel patt = new JPanel();
addBorder(patt,"Patterns");
GridLayout layout = new GridLayout(1,0);
patt.setLayout(layout);//sets a correct new layout
java.util.List<Pattern> prenames = store.getPatternsNameSorted();
Pattern[] names = makearray(prenames);//uses the helper function to transfer information across to get the types right
JList list = new JList(names);
list.addListSelectionListener(new ListSelectionListener() {//WHERE THE LISTENER IS ADDED
@Override
public void valueChanged(ListSelectionEvent e) {
JList<Pattern> listII = (JList<Pattern>) e.getSource();
Pattern p = listII.getSelectedValue();
ArrayList<World> newlist = new ArrayList<>(); //using clear() caused a nullpointer exeption, easy workaround
cachedWorlds = newlist;
if ((p.getHeight()*p.getWidth())>64) {
try {
ArrayWorld arrayinput = new ArrayWorld(p);
world=arrayinput;
cachedWorlds.add(world);
}
catch (PatternFormatException pfe) {
System.out.println("Oops, a Pattern Format Exception was detected!");
}
}
else {
try {
PackedWorld packedinput = new PackedWorld(p);
world=packedinput;
cachedWorlds.add(world);
}
catch (PatternFormatException pf) {
System.out.println("Oops, a Pattern Format Exception was detected!");
}
}
playing=true;
runOrPause();
gamePanel.display(world);
}
});
patt.add(new JScrollPane(list));
return patt;
}
private JPanel createControlPanel() {
JPanel ctrl = new JPanel();
addBorder(ctrl,"Controls");
GridLayout layout = new GridLayout(1,0);
ctrl.setLayout(layout);//sets a new layout so the buttons are the right size
JButton backbutton = new JButton("< Back");//Adding the listener directly in this bit
backbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
playing=true;
runOrPause();
moveBack();
}
});
ctrl.add(backbutton, BorderLayout.WEST);
playButton = new JButton("Play");
playButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
runOrPause();
}
});
ctrl.add(playButton, BorderLayout.CENTER);
JButton forwardbutton = new JButton("Forward >");//Direct addition of listener again
forwardbutton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
playing=true;
runOrPause();
moveForward();
}
catch(CloneNotSupportedException exc) {
System.out.print("Oops, a CloneNotSupportedException was thrown!");
}
}
});
ctrl.add(forwardbutton, BorderLayout.EAST);//adds the buttons needed
return ctrl;
}
private void runOrPause() {
if (playing) {
timer.cancel();
playing=false;
playButton.setText("Play");
}
else {
playing=true;
playButton.setText("Stop");
timer = new java.util.Timer(true);
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
moveForward();
}
catch (CloneNotSupportedException e) {
System.out.println("Oops, a Clone Not Supported Exception was caught!");
}
}
}, 0, 500);
}
}
public static void main(String[] args) throws PatternFormatException , IOException{
try {
String url = "https://www.cl.cam.ac.uk/teaching/1819/OOProg/ticks/life.txt";
PatternStore patterns = new PatternStore(url);
GUILife gui = new GUILife(patterns);
gui.setVisible(true);
}
catch (PatternFormatException e) {
System.out.println("Another exception...");
}
}
private World copyWorld(boolean useCloning) throws CloneNotSupportedException {//copypasted
World copy = null;
if (useCloning==false) {
if (world instanceof PackedWorld) {
copy = new PackedWorld(world);
}
else if (world instanceof ArrayWorld){
ArrayWorld aw = (ArrayWorld)world;
copy = new ArrayWorld(aw);
}
}
else {
copy = (World) world.clone();
}
return copy;
}
public void moveBack() {
int desiredGen = world.getGenerationCount()-1;
for (int i=0;i<cachedWorlds.size();i++) {
if ((cachedWorlds.get(i).getGenerationCount())==desiredGen) {
world=cachedWorlds.get(i);
}
}
gamePanel.display(world);
}
public void moveForward() throws CloneNotSupportedException{
int desiredGen=world.getGenerationCount()+1;
boolean found=false;
World newworld=null;
for (int i=0;i<cachedWorlds.size();i++) {
if ((cachedWorlds.get(i).getGenerationCount())==desiredGen) {
world=cachedWorlds.get(i);
found=true;
}
}
if (found) {
}
else {
world=copyWorld(true);
world.nextGeneration();
cachedWorlds.add(world);//REMOVED THE PRINT METHOD FROM THESE
}
gamePanel.display(world);
}
}