Thursday, August 28, 2008

Simple Drawing App....Using Drawing API

Started school this week and so far, looks to be a fun semester. I have Multimedia Programming ( Actionscript 3 Class). Today we just went over basics and definitions like classes and properties, etc. While that was going on I tried to make a quick drawing app so I can paint a little bit. Its all using the drawing API which I learned mostly from Keith Peters book. Here is what I got in the hour.





Clemente's simple Drawing App.





[kml_flashembed movie="http://blog.kreativeking.com/downloads/examples/drawApp/draw.swf" height="400" width="550" /]


Source



import flash.display.Sprite;

var thePencil:pencil = new pencil();
var color:uint = 0x000000;
var mySprite:Sprite = new Sprite();
var toolBar:Sprite = new bar();
addChild(mySprite);
addChild(toolBar);
toolBar.x = 487;


stage.addEventListener(Event.ENTER_FRAME, showMouse);
stage.addEventListener(MouseEvent.MOUSE_DOWN, setPoint);

init();

function init():void
{
var reddish:Sprite = new Sprite();
reddish.buttonMode = true;
reddish.useHandCursor = true;
addChild(reddish);
reddish.name = "reddish";
reddish.x = 500;
reddish.y = 30;
reddish.graphics.beginFill(0xEF0038, 1);
reddish.graphics.drawRect(0, 0, 20, 20);
reddish.graphics.endFill();

reddish.addEventListener(MouseEvent.CLICK, changeColor);
reddish.addEventListener(MouseEvent.ROLL_OVER, hideMouse);
reddish.addEventListener(MouseEvent.ROLL_OUT, showMouseFunction);

var bluish:Sprite = new Sprite();
bluish.buttonMode = true;
bluish.useHandCursor = true;
addChild(bluish);
bluish.name = "bluish"
bluish.x = 500;
bluish.y = 60;
bluish.graphics.beginFill(0x3399FF, 1);
bluish.graphics.drawRect(0, 0, 20, 20);
bluish.graphics.endFill();

bluish.addEventListener(MouseEvent.CLICK, changeColor);
bluish.addEventListener(MouseEvent.ROLL_OVER, hideMouse);
bluish.addEventListener(MouseEvent.ROLL_OUT, showMouseFunction);

var greenish:Sprite = new Sprite();
greenish.buttonMode = true;
greenish.useHandCursor = true;
addChild(greenish);
greenish.name = "greenish"
greenish.x = 500;
greenish.y = 90;
greenish.graphics.beginFill(0x00CC00, 1);
greenish.graphics.drawRect(0, 0, 20, 20);
greenish.graphics.endFill();

greenish.addEventListener(MouseEvent.CLICK, changeColor);
greenish.addEventListener(MouseEvent.ROLL_OVER, hideMouse);
greenish.addEventListener(MouseEvent.ROLL_OUT, showMouseFunction);

var black:Sprite = new Sprite();
black.buttonMode = true;
black.useHandCursor = true;
addChild(black);
black.name = "black"
black.x = 500;
black.y = 120;
black.graphics.beginFill(0x000000, 1);
black.graphics.drawRect(0, 0, 20, 20);
black.graphics.endFill();

black.addEventListener(MouseEvent.CLICK, changeColor);
black.addEventListener(MouseEvent.ROLL_OVER, hideMouse);
black.addEventListener(MouseEvent.ROLL_OUT, showMouseFunction);
}

function setPoint(e:MouseEvent):void
{
mySprite.graphics.lineStyle(2, color, 1);
mySprite.graphics.moveTo(mouseX, mouseY);
stage.addEventListener(Event.ENTER_FRAME, drawIt);
}

function drawIt(e:Event):void
{
mySprite.graphics.lineTo(mouseX, mouseY);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDraw);
}

function stopDraw(e:Event):void
{
stage.removeEventListener(Event.ENTER_FRAME, drawIt);
}

function changeColor(e:MouseEvent):void
{
switch(e.target.name)
{
case "reddish":
color = 0xEF0038;
break;

case "bluish":
color = 0x3399FF;
break;

case "greenish":
color = 0x00CC00;
break;

case "black":
color = 0x000000;
break;
}
}

function showMouse(e:Event):void
{
addChild(thePencil);
thePencil.x = mouseX;
thePencil.y = mouseY;
thePencil.scaleX = .4;
thePencil.scaleY = .4;
Mouse.hide();
}

function showMouseFunction(e:MouseEvent):void
{
stage.addEventListener(Event.ENTER_FRAME, showMouse);
}

function hideMouseFunction():void
{
removeChild(thePencil);
Mouse.show();
}

function hideMouse(e:Event):void
{
stage.removeEventListener(Event.ENTER_FRAME, showMouse);
hideMouseFunction();
}



Download the FLA here

No comments:

Post a Comment