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 graphic_flex_draw_classes_DrawingBitmapStrokes extends Sprite { /** * Constructor. Sets up stage listener. */ public function 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. * * @param 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. * * @param 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. * * @param event Event dispatched by stage. */ private function onStageMouseMove(event:MouseEvent):void { graphics.lineTo(stage.mouseX, stage.mouseY); event.updateAfterEvent(); } } }