SimplePaint
package {
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
[SWF(backgroundColor="#FFFFFF", frameRate="25",width="600",height="400" )]
public class SimplePaint extends Sprite {
public var rectBtn:SimpleButton = new SimpleButton();
public var circleBtn:SimpleButton = new SimpleButton();
public var clearBtn:SimpleButton = new SimpleButton();
public var base:Sprite = new Sprite();
public var canvas:Sprite = new Sprite();
public var tempSp:Sprite = new Sprite();
public var startX:int;
public var startY:int;
public var endX:int;
public var endY:int;
public function SimplePaint():void {
base.graphics.lineStyle(3);
base.graphics.drawRect(10, 45, 500, 380);
canvas.graphics.beginFill(0xFFFFFF);
canvas.graphics.drawRect(10, 45, 500, 380);
canvas.graphics.endFill();
addChild(base);
base.addChild(canvas);
addChild(tempSp);
//
var btnRect1:Rectangle = new Rectangle(10, 10, 40, 30);
var btnRect2:Rectangle = new Rectangle(55, 10, 40, 30);
var btnRect3:Rectangle = new Rectangle(340, 10, 40, 30);
//
var rectUp:Shape = new Shape();
drawButtonState(rectUp, btnRect1, 0xCCCCCC);
rectUp.graphics.drawRect(18, 17, 24, 16);
var rectOver:Shape = new Shape();
drawButtonState(rectOver, btnRect1, 0x9999FF);
rectOver.graphics.drawRect(18, 17, 24, 16);
var rectDown:Shape = new Shape();
drawButtonState(rectDown, btnRect1, 0x3333FF);
rectDown.graphics.drawRect(18, 17, 24, 16);
var rectHit:Shape = new Shape();
drawButtonState(rectHit, btnRect1, 0x000000);
rectHit.graphics.drawRect(18, 17, 24, 16);
rectBtn.upState = rectUp;
rectBtn.overState = rectOver;
rectBtn.downState = rectDown;
rectBtn.hitTestState = rectHit;
rectBtn.addEventListener(MouseEvent.CLICK, drawRect1);
addChild(rectBtn);
//
var circleUp:Shape = new Shape();
drawButtonState(circleUp, btnRect2, 0xCCCCCC);
circleUp.graphics.drawCircle(74, 25, 10);
var circleOver:Shape = new Shape();
drawButtonState(circleOver, btnRect2, 0x9999FF);
circleOver.graphics.drawCircle(74, 25, 10);
var circleDown:Shape = new Shape();
drawButtonState(circleDown, btnRect2, 0x3333FF);
circleDown.graphics.drawCircle(74, 25, 10);
var circleHit:Shape = new Shape();
drawButtonState(circleHit, btnRect2, 0x000000);
circleHit.graphics.drawCircle(74, 25, 10);
circleBtn.upState = circleUp;
circleBtn.overState = circleOver;
circleBtn.downState = circleDown;
circleBtn.hitTestState = circleHit;
circleBtn.addEventListener(MouseEvent.CLICK, drawCircle1);
addChild(circleBtn);
//
var clearUp:Shape = new Shape();
drawButtonState(clearUp, btnRect3, 0xCCCCCC);
var clearOver:Shape = new Shape();
drawButtonState(clearOver, btnRect3, 0x9999FF);
var clearDown:Shape = new Shape();
drawButtonState(clearDown, btnRect3, 0x3333FF);
var clearHit:Shape = new Shape();
drawButtonState(clearHit, btnRect3, 0x000000);
clearBtn.upState = clearUp;
clearBtn.overState = clearOver;
clearBtn.downState = clearDown;
clearBtn.hitTestState = clearHit;
clearBtn.addEventListener(MouseEvent.CLICK, clear);
addChild(clearBtn);
function drawButtonState(shape:Shape, rect:Rectangle, color:uint):void {
shape.graphics.lineStyle(2, 0x000000);
shape.graphics.beginFill(color);
shape.graphics.drawRect(rect.x, rect.y, rect.width, rect.height);
shape.graphics.endFill();
}
}
//
public function drawRect1(e:MouseEvent):void {
canvas.addEventListener(MouseEvent.MOUSE_DOWN, startDrawRect);
canvas.removeEventListener(MouseEvent.MOUSE_DOWN, startDrawCircle);
}
public function startDrawRect(e:MouseEvent):void {
startX = e.localX;
startY = e.localY;
addEventListener(MouseEvent.MOUSE_MOVE, drawRect);
}
public function drawRect(e:MouseEvent):void {
endX = e.localX;
endY = e.localY;
tempSp.graphics.clear();
tempSp.graphics.lineStyle(2);
tempSp.graphics.drawRect(startX, startY, endX - startX, endY - startY);
addEventListener(MouseEvent.MOUSE_UP, stopDrawRect);
}
public function stopDrawRect(e:MouseEvent):void {
endX = e.localX;
endY = e.localY;
removeEventListener(MouseEvent.MOUSE_MOVE, drawRect);
removeEventListener(MouseEvent.MOUSE_UP, stopDrawRect);
canvas.graphics.lineStyle(2);
canvas.graphics.drawRect(startX, startY, endX - startX, endY - startY);
}
//
public function drawCircle1(e:MouseEvent):void {
canvas.addEventListener(MouseEvent.MOUSE_DOWN, startDrawCircle);
canvas.removeEventListener(MouseEvent.MOUSE_DOWN, startDrawRect);
}
public function startDrawCircle(e:MouseEvent):void {
startX = e.localX;
startY = e.localY;
addEventListener(MouseEvent.MOUSE_MOVE, drawCircle);
}
public function drawCircle(e:MouseEvent):void {
endX = e.localX;
endY = e.localY;
tempSp.graphics.clear();
tempSp.graphics.lineStyle(2);
tempSp.graphics.drawEllipse(startX, startY, endX - startX, endY - startY);
addEventListener(MouseEvent.MOUSE_UP, stopDrawCircle);
}
public function stopDrawCircle(e:MouseEvent):void {
endX = e.localX;
endY = e.localY;
removeEventListener(MouseEvent.MOUSE_MOVE, drawCircle);
removeEventListener(MouseEvent.MOUSE_UP, stopDrawCircle);
canvas.graphics.lineStyle(2);
canvas.graphics.drawEllipse(startX, startY, endX - startX, endY - startY);
}
public function clear(e:MouseEvent):void {
canvas.graphics.clear();
canvas.graphics.beginFill(0xFFFFFF);
canvas.graphics.drawRect(10, 45, 500, 380);
canvas.graphics.endFill();
canvas.removeEventListener(MouseEvent.MOUSE_DOWN, startDrawCircle);
canvas.removeEventListener(MouseEvent.MOUSE_DOWN, startDrawRect);
tempSp.graphics.clear();
}
}
}