Quad Painter
Quad Painter
*
* Objective:
* To draw a picture by recursive refining areas of colour.
*
* Click to activate.
* Up and Down arrow keys increase and decrease the brightness
* of the square the mouse is currently over.
* Click a square to break it into 4 quadrants.
*
// forked from Jareth's Divide Painter
/** Quad Painter
*
* Objective:
* To draw a picture by recursive refining areas of colour.
*
* Click to activate.
* Up and Down arrow keys increase and decrease the brightness
* of the square the mouse is currently over.
* Click a square to break it into 4 quadrants.
*
*/
package {
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.ui.Keyboard;
public class DividePainter extends Sprite {
private var canvas:Sprite;
private var click:Point;
private var selected:int;
private var rects:Array;
private var colours:Array;
private var clips:Array;
private var firstClick:Boolean = false;
public function DividePainter() {
canvas = new Sprite();
addChildAt(canvas, 0);
rects = [new Rectangle(0, 0, stage.stageWidth, stage.stageHeight)];
colours = [0xFFFFFF];
clips = [canvas.addChild(new Sprite())];
selected = 0;
clips[selected].name = "" + selected;
clips[selected].addEventListener(MouseEvent.MOUSE_OVER, selectClip, false, 0, true);
clips[selected].addEventListener(MouseEvent.CLICK, splitClip, false, 0, true);
drawRect(selected);
stage.addEventListener(KeyboardEvent.KEY_DOWN, reset, false, 0, true);
}
private function drawRect(index:int):void{
var rect:Rectangle = rects[index];
var colour:Number = colours[index];
var clip:Sprite = clips[index];
clip.graphics.clear();
clip.graphics.beginFill(colour);
clip.graphics.lineStyle(1, 0xCCCCCC);
clip.graphics.drawRect(rect.left, rect.top, rect.width, rect.height);
}
private function splitClip(e:MouseEvent):void {
if (firstClick) {
var topLeft:Rectangle = rects[selected].clone();
var topRight:Rectangle = rects[selected].clone();
var bottomLeft:Rectangle = rects[selected].clone();
var bottomRight:Rectangle = rects[selected].clone();
var halfWidth:int = rects[selected].width / 2;
var halfHeight:int = rects[selected].height / 2;
var colour:Number = colours[selected];
canvas.removeChild(clips[selected]);
topLeft.right -= halfWidth;
topLeft.bottom -= halfHeight;
topRight.left += halfWidth;
topRight.bottom -= halfHeight;
bottomLeft.right -= halfWidth;
bottomLeft.top += halfHeight;
bottomRight.left += halfWidth;
bottomRight.top += halfHeight;
pushRect(topLeft, colour);
pushRect(topRight, colour);
pushRect(bottomLeft, colour);
pushRect(bottomRight, colour);
}
firstClick = true;
}
private function pushRect(rect:Rectangle, colour:Number):void {
rects.push(rect);
colours.push(colour);
clips.push(canvas.addChild(new Sprite()));
var index:int = clips.length-1;
clips[index].name = "" + (index);
clips[index].addEventListener(MouseEvent.CLICK, splitClip, false, 0, true);
clips[index].addEventListener(MouseEvent.MOUSE_OVER, selectClip, false, 0, true);
drawRect(index);
}
private function changeColour(e:MouseEvent):void {
selected = e.currentTarget.name;
colours[selected] = 0;
drawRect(selected);
}
private function selectClip(e:MouseEvent):void {
selected = e.currentTarget.name;
}
private function reset(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.SPACE) {
rects = [new Rectangle(0, 0, stage.stageWidth, stage.stageHeight)];
colours = [0xFFFFFF];
clips = [canvas.addChild(new Sprite())];
selected = 0;
drawRect(selected);
} else if (e.keyCode == Keyboard.UP) {
colours[selected] += colours[selected] < 0xFFFFFF ? 0x070707 : 0x000000;
drawRect(selected);
} else if (e.keyCode == Keyboard.DOWN) {
colours[selected] -= colours[selected] > 0x060606 ? 0x070707 : 0x000000;
drawRect(selected);
}
}
}
}