package
{
import flash.display.GraphicsPath;
import flash.display.GraphicsSolidFill;
import flash.display.GraphicsStroke;
import flash.display.IGraphicsData;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
public class test005 extends Sprite
{
private static const INIT_THICKNESS:uint = 5;
private static const MAX_THICKNESS:uint = 50;
private static const MIN_THICHNESS:uint = 1;
private var thickness:uint = 0;
private var drawing:Boolean = false;
private var commands:Vector.<IGraphicsData>=null;
private var line_style:GraphicsStroke = null;
private var current_path:GraphicsPath = null;
public function test005()
{
init();
}
private function init():void{
var color:uint = Math.random() * 0xFFFFFF;
thickness = INIT_THICKNESS;
this.graphics.lineStyle(thickness,color);
line_style = new GraphicsStroke(thickness);
line_style.fill = new GraphicsSolidFill(color);
commands = new Vector.<IGraphicsData>();
commands.push(line_style);
stage.addEventListener(MouseEvent.MOUSE_DOWN,mouse_down);
stage.addEventListener(MouseEvent.MOUSE_UP,mouse_up);
stage.addEventListener(KeyboardEvent.KEY_DOWN,key_down);
}
private function mouse_down(e:MouseEvent):void{
drawing = true;
var x:Number = stage.mouseX;
var y:Number = stage.mouseY;
current_path = new GraphicsPath();
current_path.moveTo(x,y);
commands.push(current_path);
this.graphics.moveTo(x,y);
stage.addEventListener(MouseEvent.MOUSE_MOVE,mouse_move);
}
private function mouse_up(e:MouseEvent):void{
drawing = false;
stage.removeEventListener(MouseEvent.MOUSE_MOVE,mouse_move);
}
private function key_down(e:KeyboardEvent):void{
if(drawing==false){
switch(e.keyCode){
case Keyboard.UP:
if(thickness<MAX_THICKNESS){
line_style.thickness = ++ thickness;
redrawPath();
}
break;
case Keyboard.DOWN:
if(thickness>MIN_THICHNESS){
line_style.thickness = -- thickness;
redrawPath();
}
break;
case Keyboard.SPACE:
line_style.fill = new GraphicsSolidFill(Math.random()*0xFFFFFF);
redrawPath();
break;
}
}
}
private function mouse_move(e:MouseEvent):void{
var x:Number = stage.mouseX;
var y:Number = stage.mouseY;
current_path.lineTo(x,y);
this.graphics.lineTo(x,y);
e.updateAfterEvent();
}
private function redrawPath():void{
this.graphics.clear();
this.graphics.drawGraphicsData(commands);
}
}
}