-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathElementaryCellular.java
More file actions
196 lines (175 loc) · 7.21 KB
/
ElementaryCellular.java
File metadata and controls
196 lines (175 loc) · 7.21 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
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.SwingUtilities;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
/**
* A Swing component that displays one-dimensional elementary cellular
* automata with time flowing downward, as is customary in this genre.
* <p>
* The 256 possible rules are selected with a slider (Wolfram numbering),
* and a checkbox toggles the Fredkin XOR variant, which XORs each cell
* with its state from two generations ago — producing self-similar fractal
* patterns from many otherwise "boring" rules.
* <p>
* Each cell's next state is determined by its own state and those of its
* two neighbours in the previous generation. The three-bit neighbourhood
* encodes to a number 0–7, and the corresponding bit of the rule number
* gives the output. The grid wraps horizontally (toroidal boundary).
*
* @see <a href="https://en.wikipedia.org/wiki/Elementary_cellular_automaton">
* Wikipedia: Elementary Cellular Automaton</a>
* @author Ilkka Kokkarinen
*/
public class ElementaryCellular extends JPanel {
private static final int CELL_SIZE = 2; // pixels per cell
private static final Color ALIVE = Color.BLACK;
private static final Color DEAD = Color.WHITE;
private final int cols;
private final int rows;
private final boolean[][] grid; // grid[x][y]
private final BufferedImage image;
private int rule = 110;
private final JLabel ruleLabel = new JLabel(formatRule(rule));
private final JCheckBox fredkinBox = new JCheckBox("Fredkin XOR");
// -----------------------------------------------------------------------
// Construction
// -----------------------------------------------------------------------
/**
* Create a cellular automaton display with the given grid dimensions.
* The initial configuration is a single live cell at the top centre.
*
* @param cols number of cells across
* @param rows number of generations (rows) to display
*/
public ElementaryCellular(int cols, int rows) {
this.cols = cols;
this.rows = rows;
this.grid = new boolean[cols][rows];
this.image = new BufferedImage(
cols * CELL_SIZE, rows * CELL_SIZE,
BufferedImage.TYPE_INT_RGB);
grid[cols / 2][0] = true; // seed: single cell at top centre
setLayout(new BorderLayout());
add(buildControlPanel(), BorderLayout.NORTH);
recomputeAndRepaint();
}
private JPanel buildControlPanel() {
var slider = new JSlider(0, 255, rule);
slider.setMajorTickSpacing(50);
slider.setPaintTicks(true);
// Lambdas instead of named inner listener classes.
slider.addChangeListener(_ -> {
rule = slider.getValue();
ruleLabel.setText(formatRule(rule));
recomputeAndRepaint();
});
fredkinBox.addItemListener(_ -> recomputeAndRepaint());
var controls = new JPanel();
controls.setLayout(new BoxLayout(controls, BoxLayout.LINE_AXIS));
controls.setBorder(BorderFactory.createEmptyBorder(4, 8, 4, 8));
controls.add(new JLabel("Rule: "));
controls.add(ruleLabel);
controls.add(Box.createHorizontalStrut(8));
controls.add(slider);
controls.add(Box.createHorizontalStrut(12));
controls.add(fredkinBox);
return controls;
}
private static String formatRule(int rule) {
// Show both decimal and binary so students see which bits are set.
return "%d (%s)".formatted(rule,
String.format("%8s", Integer.toBinaryString(rule))
.replace(' ', '0'));
}
// -----------------------------------------------------------------------
// Cellular automaton logic
// -----------------------------------------------------------------------
/**
* Evaluate cell (x, y) from its row-(y−1) neighbourhood.
* The three-bit neighbourhood [left, centre, right] is read as
* a number 0–7, and the corresponding bit of {@code rule} gives
* the Wolfram output. If the Fredkin variant is enabled, the result
* is XORed with the cell's state two generations ago.
*/
private boolean evaluateCell(int x, int y) {
int neighbourhood =
(grid[(x + cols - 1) % cols][y - 1] ? 4 : 0)
| (grid[x][y - 1] ? 2 : 0)
| (grid[(x + 1) % cols][y - 1] ? 1 : 0);
boolean wolfram = (rule & (1 << neighbourhood)) != 0;
if (fredkinBox.isSelected() && y > 1) {
return wolfram ^ grid[x][y - 2];
}
return wolfram;
}
/**
* Recompute the entire grid from the seed row and render it
* into the offscreen image.
*/
private void recomputeAndRepaint() {
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
if (y > 0) {
grid[x][y] = evaluateCell(x, y);
}
// Paint the cell into the image.
int rgb = grid[x][y] ? ALIVE.getRGB() : DEAD.getRGB();
int px = x * CELL_SIZE;
int py = y * CELL_SIZE;
for (int dy = 0; dy < CELL_SIZE; dy++) {
for (int dx = 0; dx < CELL_SIZE; dx++) {
image.setRGB(px + dx, py + dy, rgb);
}
}
}
}
repaint();
}
// -----------------------------------------------------------------------
// Rendering
// -----------------------------------------------------------------------
@Override
public Dimension getPreferredSize() {
// Let the layout manager know how big we'd like to be.
int controlHeight = 40; // approximate height of control panel
return new Dimension(
cols * CELL_SIZE,
rows * CELL_SIZE + controlHeight);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
var g2 = (Graphics2D) g;
g2.setRenderingHint(
RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
// Draw the image below the control panel.
int imageY = getHeight() - rows * CELL_SIZE;
g2.drawImage(image, 0, imageY, this);
}
// -----------------------------------------------------------------------
// Entry point
// -----------------------------------------------------------------------
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
var frame = new JFrame("Elementary Cellular Automata");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.add(new ElementaryCellular(500, 500));
frame.pack();
frame.setLocationRelativeTo(null); // centre on screen
frame.setVisible(true);
});
}
}