package { import flash.display.Shape; import flash.display.Sprite; import flash.events.MouseEvent; import flash.geom.Point; [SWF(width=550, height=400, backgroundColor=0xFFFFFF)] /** * Demonstrates how to draw straight lines with the drawing API. */ public class graphic_flex_image_effects_01_Flex_DrawingStraightLines extends Sprite { private var _currentShape:Shape; private var _color:uint; private var _startPosition:Point; /** * Constructor. Sets up listeners for mouse events. */ public function graphic_flex_image_effects_01_Flex_DrawingStraightLines() { stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown); stage.addEventListener(MouseEvent.MOUSE_UP, onStageMouseUp); } /** * Draws a line from the start position to the current mouse position. */ private function drawLine():void { _currentShape.graphics.clear(); _currentShape.graphics.lineStyle(3, _color); _currentShape.graphics.moveTo(_startPosition.x, _startPosition.y); _currentShape.graphics.lineTo(stage.mouseX, stage.mouseY); } /** * Handler for when the stage is clicked. This creates a new shape for the current line, * saves the starting posiiton and sets up a listener for when the mouse moves. * * @param event Event dispatched by the stage. */ private function onStageMouseDown(event:MouseEvent):void { _color = Math.random()*0xFFFFFF; _currentShape = new Shape(); addChild(_currentShape); _startPosition = new Point(stage.mouseX, stage.mouseY); stage.addEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove); } /** * Handler for when the mouse is released. This removes the listener for when the mouse moves. * * @param event Event dispatched by the stage. */ private function onStageMouseUp(event:MouseEvent):void { stage.removeEventListener(MouseEvent.MOUSE_MOVE, onStageMouseMove); } /** * Handler for when the mouse moves. This updates the drawn line based on the current mouse position. * * @param event Event dipsatched by the stage. */ private function onStageMouseMove(event:MouseEvent):void { drawLine(); event.updateAfterEvent(); } } }