package { import flash.display.Bitmap; import flash.display.Loader; import flash.display.LoaderInfo; import flash.display.Sprite; import flash.events.Event; import flash.net.URLRequest; /** * Abstract base class for bitmap filter tests requiring loading of an image. */ public class graphic_flex_image_effects_02_Flex_ImageFilterTest extends Sprite { protected var _bitmap:Bitmap; /** * Constructor. Initiates load of image. * * @param path The path to the image to load. If no path is provided, * a default image is used. */ public function graphic_flex_image_effects_02_Flex_ImageFilterTest(path:String=null) { loadImage(path || "graphic-flex-image-effects-02-assets-goat.jpg"); } /** * Loads an image. * * @param imagePath The path to the image to load. */ private function loadImage(imagePath:String):void { var loader:Loader = new Loader(); // just checks for success; a more robust class should also check for failures loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoaded); loader.load(new URLRequest(imagePath)); } /** * Handler for when image loads successfully. This centers the bitmap on the stage * and calls the applyFilter() method. * * @param event Event dispatched by LoaderInfo. */ private function onImageLoaded(event:Event):void { var loaderInfo:LoaderInfo = event.target as LoaderInfo; loaderInfo.removeEventListener(Event.COMPLETE, onImageLoaded); _bitmap = loaderInfo.content as Bitmap; _bitmap.x = stage.stageWidth/2 - _bitmap.width/2; _bitmap.y = stage.stageHeight/2 - _bitmap.height/2; addChild(_bitmap); applyFilter(); } /** * Abstract method to be overridden by child classes in order to apply * a bitmap filter to the loaded bitmap. */ protected function applyFilter():void {} } }