-
Notifications
You must be signed in to change notification settings - Fork 0
/
Automata.java
364 lines (313 loc) · 13.7 KB
/
Automata.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
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
/**
Class from which the main applet is run.
This class initializes the applet and the automatonCanvas, and stores all
state necessary to run the applet.
Totalistic cellular automata are implemented here roughly as described by
Wolfram at http://mathworld.wolfram.com/TotalisticCellularAutomaton.html
(accessed Dec 7, 2018).
Git repo: https://github.com/tobyweed/totalistic_cellular_automata
CS 201 Final Project - Totalistic Cellular Automata
Danny Grubbs-Donovan and Toby Weed
**/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
import java.awt.Color;
// We know you said maybe don't use too many external packages, but the app is
// just sooo much better with sliders and we couldn't find a way to implement them
// without Swing
import javax.swing.JSlider;
import javax.swing.event.*;
import javax.swing.*;
import javax.swing.text.*;
@SuppressWarnings("serial") // to avoid Eclipse warning
public class Automata extends Applet implements ActionListener, ChangeListener, ItemListener {
// FIELDS ==================================================================
private AutomatonCanvas ac; // automaton display
// Variable components
private Choice kChoice;
private JFormattedTextField decCodeField;
private JSlider decCodeSlider;
private JSlider rZeroColorField, gZeroColorField, bZeroColorField;
private JSlider rKColorField, gKColorField, bKColorField;
private Button runButton, zoomIn, zoomOut, randomInit, singleInit;
// Automaton state
// Note on initialization: it's alright to just set the fields here (not in a
// constructor) because we only want to initialize the applet once (on load),
// never again
private int zoom = 5; //How many pixels is one edge of one cell
private boolean random = false; //Should the first generation be randomized?
private int k = 3; //the number of possible states for each cell
private int ruleCode = 23324; //the decimal value of the rule code
private String[] kAryRuleCode; //the k-ary value of the rule code
private Vector<Integer> randomConfig = new Vector<Integer>(); //A vector of cell with random states
private Color zeroColor = Color.white; //Default color bounds.
private Color kColor = Color.black;
// SETUP ===================================================================
// Initialize the applet
public void init() {
setFont(new Font("TimesRoman", Font.BOLD, 14));
setLayout(new BorderLayout());
BorderLayout b = new BorderLayout();
Panel automata = new Panel(b); //Frame
kAryRuleCode = Automaton.intToKAry(ruleCode,k);
automata.add("North",Specs());
Panel sim = new Panel(new BorderLayout());
ac = new AutomatonCanvas(this);
sim.add(ac);
automata.add("Center",sim);
automata.add("South",Controls());
//Set initial state for displays
rZeroColorField.setValue(254);
gZeroColorField.setValue(254);
bZeroColorField.setValue(254);
decCodeField.setValue(777);
decCodeSlider.setValue(777);
add(automata);
}
// COMPONENTS ==============================================================
// UI to change k & decimal rule code. Also displays visualization of kary
// rule code.
private Panel Specs() {
Panel specs = new Panel(new FlowLayout());
specs.setBackground(new Color(145, 153, 186));
// menu to choose k
Label kLabel = new Label("Number of Colors:");
kChoice = new Choice();
for(int n = 2; n <= 20; n++) {
kChoice.addItem("" + n);
}
kChoice.addItemListener(this);
kChoice.setForeground(Color.black);
kChoice.select(1);
Panel kChoicePanel = new Panel(new BorderLayout());
kChoicePanel.add("West", kLabel);
kChoicePanel.add("Center", kChoice);
// Decimal rule code slider and text field
Label codeLabel = new Label("Decimal Rule Code:");
//This works in theory but in practice the max possible value for the
//slider/textbox combo is 2147483647
int numPoss = (int)Math.pow(k,(3*k-2));
decCodeSlider = new JSlider(JSlider.HORIZONTAL,0,numPoss,0); //slider for rule code entry
decCodeSlider.addChangeListener(this);
NumberFormatter codeLimits = new NumberFormatter();
codeLimits.setMinimum(0);
codeLimits.setMaximum(numPoss);
decCodeField = new JFormattedTextField(codeLimits); //field for rule code entry
decCodeField.addActionListener(this);
decCodeField.setColumns(7);
decCodeField.setValue(0);
Panel codePanel = new Panel();
codePanel.setLayout(new FlowLayout());
codePanel.add(codeLabel);
codePanel.add(decCodeSlider);
codePanel.add(decCodeField);
specs.add(kChoicePanel);
specs.add(codePanel);
return specs;
}
//Where we run the applet
//adds the toggle random, zoom, and run buttons on the bottom browser
protected Panel Controls() {
Panel controls = new Panel(new GridLayout(1,3,0,5));
controls.setBackground(new Color(145, 153, 186));
// Button for a random starting state.
randomInit = new Button("Toggle Random Initialization");
randomInit.addActionListener(this);
// run button
runButton = new Button("Run");
runButton.addActionListener(this);
// zoom panel
Label zoomLabel = new Label("Zoom:");
zoomIn = new Button("+");
zoomIn.addActionListener(this);
Label zoomSlash = new Label("/");
zoomOut = new Button("-");
zoomOut.addActionListener(this);
Panel zoomPanel = new Panel(new FlowLayout());
zoomPanel.add(zoomLabel);
zoomPanel.add(zoomIn);
zoomPanel.add(zoomSlash);
zoomPanel.add(zoomOut);
// Add + return everything
controls.add(randomInit);
controls.add(runButton);
controls.add(zoomPanel);
controls.add(ColorPanel());
return controls;
}
// The panel to change color bounds
// Adds sliders to allow user to choose colors on bottom right.
// Has sliders for R,G,B for both color bounds
public Panel ColorPanel() {
Panel colorControl1 = new Panel(new GridLayout(4,2));
Panel colorControl2 = new Panel(new GridLayout(4,2));
Panel sliders1 = new Panel(new GridLayout(3,1));
Panel labels1 = new Panel(new GridLayout(3,1));
//Color sliders
rZeroColorField = new JSlider(JSlider.HORIZONTAL, 0,255,0);
rZeroColorField.addChangeListener(this);
gZeroColorField = new JSlider(JSlider.HORIZONTAL, 0,255,0);
gZeroColorField.addChangeListener(this);
bZeroColorField = new JSlider(JSlider.HORIZONTAL, 0,255,0);
bZeroColorField.addChangeListener(this);
rKColorField = new JSlider(JSlider.HORIZONTAL, 0,255,0);
rKColorField.addChangeListener(this);
gKColorField = new JSlider(JSlider.HORIZONTAL, 0,255,0);
gKColorField.addChangeListener(this);
bKColorField = new JSlider(JSlider.HORIZONTAL, 0,255,0);
bKColorField.addChangeListener(this);
//color labels
colorControl1.add(new Label(""));
colorControl1.add(new Label("Lower Color Bound"));
colorControl1.add(new Label("R:",Label.RIGHT));
colorControl1.add(rZeroColorField);
colorControl1.add(new Label("G:",Label.RIGHT));
colorControl1.add(gZeroColorField);
colorControl1.add(new Label("B:",Label.RIGHT));
colorControl1.add(bZeroColorField);
colorControl2.add(new Label(""));
colorControl2.add(new Label("Upper Color Bound"));
colorControl2.add(new Label("R:", Label.RIGHT));
colorControl2.add(rKColorField);
colorControl2.add(new Label("G:",Label.RIGHT));
colorControl2.add(gKColorField);
colorControl2.add(new Label("B:",Label.RIGHT));
colorControl2.add(bKColorField);
Panel colorPanel = new Panel(new GridLayout(1,2));
colorPanel.add(colorControl1);
colorPanel.add(colorControl2);
return colorPanel;
}
// EVENT HANDLERS ==========================================================
// action handler for buttons & TextField & color changes
public void actionPerformed(ActionEvent evt) {
if (evt.getSource() == runButton) { // set automaton state and repaint
//sync up state
k = Integer.parseInt(kChoice.getSelectedItem());
ruleCode = Integer.parseInt(decCodeField.getText());
kAryRuleCode = Automaton.intToKAry(ruleCode,k);
int rZeroValue = (rZeroColorField.getValue());
int gZeroValue = (gZeroColorField.getValue());
int bZeroValue = (bZeroColorField.getValue());
int rKValue = (rKColorField.getValue());
int gKValue = (gKColorField.getValue());
int bKValue = (bKColorField.getValue());
//new color bounds are set here
zeroColor = new Color(((float)rZeroValue/255),((float)gZeroValue/255),((float)bZeroValue/255));
kColor = new Color(((float)rKValue/255),((float)gKValue/255),((float)bKValue/255));
decCodeField.setText(Integer.toString(ruleCode));
//re-randomize randomConfig
randomConfig = new Vector<Integer>();
//repaint the automaton
ac.repaint();
} else if (evt.getSource() == zoomIn && zoom <= 50) { //zoom in. Cap at 51 pixels
zoom++;
ac.repaint();
} else if (evt.getSource() == zoomOut && zoom >= 2) { //zoom out. Cap at 1 pixel
zoom--;
ac.repaint();
} else if (evt.getSource() == decCodeField) { //Change the rulecodes & update displays
ruleCode = Integer.parseInt(decCodeField.getText());
kAryRuleCode = Automaton.intToKAry(ruleCode,k);
decCodeSlider.setValue(ruleCode);
ac.repaint();
} else if (evt.getSource() == randomInit ) { //Toggle random initialization
random = !random;
ac.repaint();
}
}
// Action handler for sliders
// handles both color and rule code sliders
// Updates automatically when they are altered.
public void stateChanged(ChangeEvent evt) {
if(evt.getSource()==decCodeSlider) { //Change the rulecodes & update displays
JSlider src = (JSlider)evt.getSource();
decCodeField.setText(Integer.toString(src.getValue()));
ruleCode = src.getValue();
kAryRuleCode = Automaton.intToKAry(ruleCode,k);
ac.repaint(); //repaints after rulecode change
} else if(evt.getSource()==rZeroColorField ||
evt.getSource()==gZeroColorField ||
evt.getSource()==bZeroColorField ||
evt.getSource()==rKColorField ||
evt.getSource()==gKColorField ||
evt.getSource()==bKColorField) { //Change the colors
int rZeroVal = rZeroColorField.getValue();
int gZeroVal = gZeroColorField.getValue();
int bZeroVal = bZeroColorField.getValue();
int rKVal = rKColorField.getValue();
int gKVal = gKColorField.getValue();
int bKVal = bKColorField.getValue();
zeroColor = new Color(((float)rZeroVal/255),((float)gZeroVal/255),((float)bZeroVal/255));
kColor = new Color(((float)rKVal/255),((float)gKVal/255),((float)bKVal/255));
ac.repaint(); //repaints after color change
}
}
// action handler for choice menu
public void itemStateChanged(ItemEvent evt) {
k = Integer.parseInt(kChoice.getSelectedItem());
// update decCodeSlider maximum value and set current value to zero
int numPoss = (int)Math.pow(k,(3*k-2));
//This works in theory but in practice the max possible value for the
//slider/textbox combo is 2147483647
decCodeSlider.setMaximum(numPoss);
decCodeSlider.setValue(0);
//update decCodeField limits & format
NumberFormatter codeLimits = new NumberFormatter();
NumberFormat nf = NumberFormat.getIntegerInstance();
nf.setGroupingUsed(false); //avoid commas in formatting
codeLimits.setFormat(nf);
codeLimits.setMinimum(0);
codeLimits.setMaximum(numPoss);
DefaultFormatterFactory limitsFactory = new DefaultFormatterFactory(codeLimits);
decCodeField.setFormatterFactory(limitsFactory);
//update state & run
ruleCode = 0;
kAryRuleCode = Automaton.intToKAry(ruleCode,k);
ac.repaint();
}
// UTILS====================================================================
// Getters -----------------------------------------------------------------
public int zoom() {
return zoom;
}
public boolean randomInit() {
return random;
}
public int getK() {
return k;
};
public Color getKColor() {
return kColor;
};
public int getCode() {
return ruleCode;
}
public String[] getKAryCode() {
return kAryRuleCode;
};
public Color getZeroColor() {
return zeroColor;
}
// Return the value of randomConfig[n]. Vector which will expand as necessary
// but remain constant if we zoom in
public int getRndmAtN(int n) {
if(n < randomConfig.size())
return randomConfig.elementAt(n);
else // if there's no value at n then make one
return setRndmAtN(n);
}
// Setters -----------------------------------------------------------------
// Put a random value from 0 to k at index n
private int setRndmAtN(int n) {
int rand = (int)(k * Math.random());
if(n < randomConfig.size())
randomConfig.add( n, rand );
else
randomConfig.add( rand );
return rand;
}
}