package
{
/**
* Demo for Belousov-Zhabotinsky- cellular automata
* Petri Leskinen, 1st July 2009
* in Espoo, Finland
*/
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.filters.ColorMatrixFilter;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.FileReference;
import flash.errors.IOError;
import flash.utils.ByteArray;
import flash.text.TextField;
import fl.controls.Button;
import fl.controls.ColorPicker;
import fl.controls.Slider;
import org.gif.encoder.GIFEncoder;
import BelousovZhabotinskyCA;
public class BelousovZhabotinskyDemo extends Sprite
{
protected var
w:int = 128,
h:int = 128,
automaton:BelousovZhabotinskyCA,
color0:uint = 0x990000,
color1:uint = 0xFFFFCC,
leftMargin:int = 120,
canvas:Sprite,
canvasWidth:int,
canvasHeight:int,
zoom:Number = 1.0,
paused:Boolean = false;
public function BelousovZhabotinskyDemo()
{
// init cellular automaton, and do 100 loops
automaton = new BelousovZhabotinskyCA(w, h);
automaton.grow(100);
// canvas on the stage
canvas= new Sprite();
addChild(canvas);
canvas.x = leftMargin;
canvasWidth = stage.stageWidth-leftMargin;
canvasHeight = stage.stageHeight;
// activate the controls
automaton.color0 = colorPicker0.selectedColor= color0;
colorPicker0.addEventListener(Event.CHANGE, changed);
automaton.color1 = colorPicker1.selectedColor= color1;
colorPicker1.addEventListener(Event.CHANGE, changed1);
sliderK1.addEventListener(Event.CHANGE, changedK1);
sliderK2.addEventListener(Event.CHANGE, changedK2);
sliderG.addEventListener(Event.CHANGE, changedG);
sliderK1.value = automaton.k1;
sliderK2.value = automaton.k2;
sliderG.value = automaton.g;
sliderZoom.addEventListener(Event.CHANGE, changedZoom);
sliderZoom.value = zoom;
checkPaused.addEventListener(Event.CHANGE, changePause);
restartButton.addEventListener(MouseEvent.CLICK, restart);
saveButton.addEventListener(MouseEvent.CLICK, buttonSave);
changeSizeButton.addEventListener(MouseEvent.CLICK, sizeChanged);
canvas.addEventListener(MouseEvent.CLICK, mouseClick);
pixeleroLink.htmlText="Pixelero";
startAnimation();
}
private function enterFrame(e:Event = null):void {
automaton.grow();
canvas.graphics.clear();
// for the tiling automaton is drawn on the canvas as a bitmapfill
canvas.graphics.beginBitmapFill(automaton, new Matrix(zoom,0,0,zoom));
canvasWidth = stage.stageWidth-leftMargin;
canvasHeight = stage.stageHeight;
if (checkTiled.selected) {
canvas.graphics.drawRect(0,0,canvasWidth,canvasHeight);
} else {
var xp:int =(canvasWidth - automaton.width)>>1,
yp:int =(canvasHeight - automaton.height)>>1;
canvas.graphics.drawRect(xp,yp,
automaton.width,
automaton.height);
}
}
// replace old automaton by a new one with same parameters
private function sizeChanged(event:MouseEvent=null):void {
var newautomaton:BelousovZhabotinskyCA =
new BelousovZhabotinskyCA(
int(stepperX.value),
int(stepperY.value),
automaton.k1,
automaton.k2,
automaton.g );
for (var mtrx:Matrix= new Matrix();mtrx.ty>1,
py:int=automaton.height>>1,
pxl:uint = automaton.getCellAt(px,py),
pxl2:uint;
while (pxl==0) {
px=automaton.width*Math.random();
py=automaton.height*Math.random();
pxl = automaton.getCellAt(px,py);
}
// ... and compare the new values
// - first increasing, back to zero, then increasing again - to sample
do {
automaton.grow();
encoder.addFrame(automaton);
} while ((pxl2= automaton.getCellAt(px,py))>pxl)
while ((pxl2= automaton.getCellAt(px,py))