topical media & game development
graphic-flex-draw-classes-DrawingBitmapStrokes.ax
graphic-flex-draw-classes-DrawingBitmapStrokes.ax
[swf]
flex
package {
import flash.display.BitmapData;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.*;
[SWF(width=550, height=400, backgroundColor=0xFFFFFF)]
Creates a bitmap to use as the fill for drawing API lines.
public class @ax-graphic-flex-draw-classes-DrawingBitmapStrokes extends Sprite {
Constructor. Sets up stage listener.
public function @ax-graphic-flex-draw-classes-DrawingBitmapStrokes() {
super();
this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(e:Event):void
{
this.init();
}
private function init():void
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp);
}
Creates the bitmap data that will be used for the lines.
private function createBrushStroke():void {
// generate random radius
var radius:uint = Math.random()*10 + 2;
var diameter:uint = radius*2;
// draw a circle that can be tiled
var shape:Shape = new Shape();
shape.graphics.beginFill(Math.random()*0xFFFFFF);
shape.graphics.drawCircle(radius, radius, radius);
shape.graphics.endFill();
// draw the shape into bitmap data
var brushStroke:BitmapData = new BitmapData(diameter, diameter, true, 0x00000000);
brushStroke.draw(shape);
// use the circle's radius as thickness and bitmap as the fill
graphics.lineStyle(diameter);
graphics.lineBitmapStyle(brushStroke);
}
Handler for when the stage is clicked. This sets up a new bitmap to be used for drawing
and sets up a listener for when the mouse moved.
parameter: event Event dispatched by stage.
private function onStageMouseDown(event:MouseEvent):void {
createBrushStroke();
graphics.moveTo(stage.mouseX, stage.mouseY);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
}
Handler for when the mouse is released on the stage.
This removes the listener for when the mouse moved.
parameter: event Event dispatched by stage.
private function onStageMouseUp(event:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove);
}
Handler for when the mouse is moved on the stage.
Draws a line to the new mouse position.
parameter: event Event dispatched by stage.
private function onStageMouseMove(event:MouseEvent):void {
graphics.lineTo(stage.mouseX, stage.mouseY);
event.updateAfterEvent();
}
}
}
(C) Æliens
27/08/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.