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
|
package Applet;
import java.awt.*;
import java.util.Random;
public class SimpleCA extends java.applet.Applet implements Runnable {
CACanvas CA;
Thread runner = null;
public void init() {
setLayout(new BorderLayout());
CA = new CACanvas();
add("Center",CA);
setBackground(Color.black);
}
public Insets insets() {
return new Insets(2,2,2,2);
}
public void run() {
CA.properties(2,3,null,null,true);
CA.set(null);
while (true) {
CA.next();
try { Thread.sleep(25); }
catch (InterruptedException e) { }
}
}
public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}
public void stop() {
if (runner != null) {
runner.stop();
runner = null;
CA.disposeOSC();
}
}
public boolean mouseDown(Event evt, int x, int y) {
if (evt.shiftDown())
CA.reset();
CA.next();
return true;
}
}
package Applet;
import java.awt.*;
class CACanvas extends Canvas {
private boolean backedUp = true;
private Image OSC = null;
private int[] rule = null;
private int ruleCt = 8;
private int states = 2;
private int neighbors = 3;
private int halfNeighborCt = 1;
private int[] startWorld = null;
private int[] world = null;
private int[] newWorld = null;
private Color[] stateColor = null;
private int width = 0;
private int height = 0;
private int lineCt = 0;
synchronized void properties(int stateCt, int neighborCt,
int[] theRules, Color[] theColors,
boolean useOffScreenCanvas) {
backedUp = useOffScreenCanvas;
states = stateCt;
if (states < 2)
states = 2;
neighbors = neighborCt;
if (neighbors % 2 == 0)
neighbors++;
if (neighborCt < 3)
neighborCt = 3;
ruleCt = 1;
for (int i=0; i<neighbors; i++) {
if (ruleCt > 32768) {
states = 2;
neighbors = 3;
ruleCt = 8;
break;
}
ruleCt = ruleCt*states;
}
halfNeighborCt = neighbors / 2;
if (theRules != null && theRules.length >= ruleCt)
rule = theRules;
else
rule = null;
if (theColors != null && theColors.length >= states)
stateColor = theColors;
else
theColors = null;
}
int getwidth() {
return (width == 0)? size().width : width;
}
void disposeOSC() {
OSC = null;
}
synchronized void set(int[] theWorld) {
if ((width != size().width || height != size().height))
OSC = null;
width = size().width;
height = size().height;
if (world == null || world.length < width)
world = new int[width];
if (newWorld == null || newWorld.length < width)
newWorld = new int[width];
if (rule == null || rule.length < ruleCt) {
rule = new int[ruleCt];
for (int i = 0; i < ruleCt; i++)
rule[i] = 1;
rule[0] = 0;
rule[ruleCt-1] = 0;
}
if (stateColor == null || stateColor.length < states) {
stateColor = new Color[states];
stateColor[0] = Color.white;
stateColor[1] = Color.black;
if (states>2) {
float hue = 0.0F;
for (int i = 2; i < states; i++) {
stateColor[i] = Color.getHSBColor(hue,1.0F,1.0F);
hue = hue + (1.0F / (states - 2));
}
}
}
if (theWorld == null) {
for (int i=0; i<width; i++)
world[i] = 0;
world[width/2] = 1;
}
else {
for (int i=0; i<width; i++)
if (i < theWorld.length && theWorld[i] >= 0 && theWorld[i] < states)
world[i] = theWorld[i];
else
world[i] = 0;
}
if (startWorld == null || startWorld.length < width)
startWorld = new int[width];
for (int i = 0; i<width; i++)
startWorld[i] = world[i];
if (backedUp && OSC == null)
OSC = createImage(width,height);
clear();
}
void reset() {
if (startWorld != null) {
if (world == null || world.length < width)
world = new int[width];
for (int i=0; i<width; i++)
if (i < startWorld.length && startWorld[i] >= 0 && startWorld[i] < states)
world[i] = startWorld[i];
else
world[i] = 0;
clear();
}
}
void setStart() {
if (world != null) {
if (startWorld == null || startWorld.length < world.length)
startWorld = new int[world.length];
for (int i=0; i<world.length; i++)
startWorld[i] = world[i];
}
}
synchronized void setCell(int cellNum, int state) {
if (world != null && cellNum >= 0 && cellNum < world.length && state >= 0 && state < states)
world[cellNum] = state;
}
synchronized void clear() {
if (OSC != null) {
Graphics g = OSC.getGraphics();
if (stateColor != null)
g.setColor(stateColor[0]);
else
g.setColor(Color.black);
g.fillRect(0,0,width,height);
g.dispose();
repaint();
}
else {
Graphics g = getGraphics();
if (stateColor != null)
g.setColor(stateColor[0]);
else
g.setColor(Color.black);
g.fillRect(0,0,width,height);
g.dispose();
}
lineCt = 0;
}
synchronized void setRules(int[] theRules) {
if (theRules.length >= ruleCt)
rule = theRules;
}
synchronized void setRule(int ruleNum, int value) {
if ((rule != null) && (value >= 0) && (value < states) && (ruleNum >= 0) && (ruleNum < rule.length))
rule[ruleNum] = value;
}
synchronized void setColors(Color[] theColors) {
if (theColors.length >= states)
stateColor = theColors;
}
synchronized void setColor(int colorNum, Color value) {
if ((stateColor != null) && (value != null) && (colorNum >= 0) && (colorNum < stateColor.length))
stateColor[colorNum] = value;
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
if (OSC != null)
g.drawImage(OSC,0,0,this);
}
synchronized void putWorld() {
Graphics g=null;
if (OSC == null)
g = getGraphics();
else
g = OSC.getGraphics();
if (lineCt >= height && g != null) {
g.copyArea(0,0,width,height,0,-1);
lineCt = height-1;
}
if (g != null) {
for (int i=0; i< width; i++) {
g.setColor(stateColor[world[i]]);
g.fillRect(i,lineCt,1,1);
}
lineCt++;
g.dispose();
}
if (OSC != null) {
g = getGraphics();
if (g != null && OSC != null) {
g.drawImage(OSC,0,0,this);
g.dispose();
}
}
}
synchronized void next() {
if (width > 0) {
for (int i=0; i < width; i++) {
int left = i - halfNeighborCt;
if (left<0)
left = width+left;
int right = i + halfNeighborCt;
if (right>=width)
right = right-width;
int k = 0;
if (left < right) {
for (int j=left; j<=right; j++)
k = states*k + world[j];
}
else {
for (int j=left; j<width; j++)
k = states*k + world[j];
for (int j=0; j<=right; j++)
k = states*k + world[j];
}
newWorld[i] = rule[k];
}
putWorld();
for (int i = 0; i < width; i++)
world[i] = newWorld[i];
}
} // next()
} // class CACanvas |
Partager