topical media & game development
#graphic-flex-image-effects-03-Flex-FloodFillTest.ax
#graphic-flex-image-effects-03-Flex-FloodFillTest.ax
[swf]
[flash]
flex
package {
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
[SWF(width=600, height=800, backgroundColor=0xFFFFFF)]
Demonstrates the BitmapData floodFill() method in a coloring book application.
public class @ax-graphic-flex-image-effects-03-Flex-FloodFillTest extends graphic_flex_image_effects_03_Flex_AbstractImageLoader {
private static const SWATCH_SIZE:uint = 50;
private var _image:BitmapData;
private var _palette:BitmapData;
private var _currentColor:uint;
Constructor. Passes path of image to load to super class.
public function @ax-graphic-flex-image-effects-03-Flex-FloodFillTest() {
super("graphic-flex-image-effects-assets-castle.gif");
}
Creates a horizontal collection of randomly colored rectangles
that serve as the palette for the line drawing.
private function createPalette():void {
_palette = new BitmapData(stage.stageWidth, SWATCH_SIZE);
var rect:Rectangle = new Rectangle(0, 0, SWATCH_SIZE, SWATCH_SIZE);
// the position of the last colored rectangle
var lastSwatch:uint = _palette.width - SWATCH_SIZE;
// runs through all the rectangles that can be drawn in the space,
// leaving a space of two rectangles at the left to display the current color
for (var x:uint = SWATCH_SIZE*2; x <= lastSwatch; x+=SWATCH_SIZE) {
// moves rectangle to next swatch position
rect.x = x;
// chooses a random color
var color:uint = Math.random()*0xFFFFFF;
// adds full alpha value
color = 0xFF << 24 | color;
_palette.fillRect(rect, color);
}
var bitmap:Bitmap = new Bitmap(_palette);
// places at bottom of stage
bitmap.y = stage.stageHeight-bitmap.height;
addChild(bitmap);
}
Called after image is loaded by super class. Adds the color palette
and the event listener for when the use clicks the stage
override protected function runPostImageLoad():void {
addChild(_loadedBitmap);
_image = _loadedBitmap.bitmapData;
createPalette();
// white color will not change white fill of initial line drawing
_currentColor = 0xFFFFFF;
stage.addEventListener(MouseEvent.MOUSE_DOWN, onStageMouseDown);
}
Handler for when the stage is clicked. Either selects a color or fills a region
of the line drawing with the current color.
parameter: event Event dispatched by the stage.
private function onStageMouseDown(event:MouseEvent):void {
var x:uint = event.localX;
var y:uint = event.localY;
// this means the palette was clicked.
if (y > _image.height) {
// grab the clicked color
_currentColor = _palette.getPixel32(x, y-_image.height);
// fill the left side of the palette with the current color
var rect:Rectangle = new Rectangle(0, 0, SWATCH_SIZE*2, SWATCH_SIZE);
_palette.fillRect(rect, _currentColor);
// else color in the line drawing with the current color
} else {
_image.floodFill(x, y, _currentColor);
}
}
}
}
(C) Æliens
04/09/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.