By paddloPayday loans

Home > Factory Method > ActionScript 3.0 Bitmap Classes: The Bitmap Factory Method At Work

ActionScript 3.0 Bitmap Classes: The Bitmap Factory Method At Work

BitmapBuilding a Bitmap Factory

The idea of taking a perfectly good graphic file and taking it apart and putting it back together again has a surprising appeal. You may wonder why one would want to do that? For me, it’s a chance to explore the Bitmap classes built into ActionScript 3.0 and see if there is some way to manage the pixel set better and learn as much as possible about the behavior of bitmap data, filters and anything else that might be useful for speeding things up.

More importantly for design pattern development is creating bitmap data objects and moving them around between participants in a pattern and making changes and additions to a program. In creating this pattern, I found it relatively easy to create a concrete product class from an abstract class used in loading and changing standard bitmap files into ActionScript 3.0 bitmap code; however, I had to spend some time calling for a bitmap object through a creator. In other words, getting something up on the stage using a Factory Method design pattern was more than just duplicating what I had done before with the Loader class and bitmap graphic files.

To get started, download the files used and take a look at the outcome by pressing the Play button.
play buttondownload this sucker

You will find examples from three types of bitmapped graphics: GIF, JPEG and PNG. Identical code was used to break them into bitmap objects from ActionScript 3.0 classes.

The Bitmap Product

The design participants to see first are the abstract and concrete bitmap products (BitMapProduct, BitMapSam). The abstract class contains the properties used in both loading the bitmaps and working with Bitmap and BitmapData properties and methods.

?View Code ACTIONSCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//Product
package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.errors.IllegalOperationError;
 
	//Abstract class
	public class BitMapProduct extends Sprite
	{
		protected var url:String;
		protected var imageLoader:Loader;
		protected var prodData:BitmapData;
		protected var prodBit:Bitmap;
 
		public function doBitMap(url:String):void
		{
			throw new IllegalOperationError("Abstract method: must be overridden in a subclass");
		}
	}
}
 
//Concrete Product
package 
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.net.URLRequest; 
 
	public class BitMapSam extends BitMapProduct
	{
		public override function doBitMap(url:String):void
		{
			imageLoader = new Loader();
			imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			imageLoader.load(new URLRequest(url));
		}
		private function onComplete(e:Event):void
		{
			prodData = e.target.content.bitmapData;
			prodBit = new Bitmap(prodData);
			addChild(prodBit);
		}
	}
}

Note that the Product (BitMapProduct) extends Sprite and the Concrete Product (BitMapSam) extends the Product. As a result the concrete product is a Sprite.

The Bitmap Factory

The Creator (factory) elements consist of the abstract factory (BitMapCreator) and the concrete factory (BitmapSamMaker). The Creator holds a reference to the Product (abstract factory) and the factory method, but that’s it. The concrete factory returns an instance of the Product using lazy instantiation.

?View Code ACTIONSCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//Creator (Factory)
package 
{
	import flash.errors.IllegalOperationError;
 
	public class BitMapCreator
	{
		protected var product:BitMapProduct;
 
		// ABSTRACT Method (must be overridden in a subclass)
		public function factoryMethod(url:String):BitMapProduct
		{
			throw new IllegalOperationError("Abstract method: must be overridden in a subclass");
			return null;
		}
	}
}
 
//Concrete Creator
package 
{
	public class BitMapSamMaker extends BitMapCreator
	{
		public override function factoryMethod(url:String):BitMapProduct
		{
			if (! product)
			{
				product=new BitMapSam();
			}
			product.doBitMap(url);
			return product;
		}
	}
}

As you can see, both Creator classes are pretty simple. Most of the work is done in the concrete product, and in it you can see the way in which ActionScript 3.0 Bitmap objects can be employed.

The Client

It took a while to work out how to bring in the loaded bitmap data through the Client, and once resolved, it turned out to be fairly simple. The Client must pass a string with the name of the bitmap graphic to load. I used three buttons to load an example of each type of graphic to illustrate its functionality with more than a single kind of bitmap graphic.

?View Code ACTIONSCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//Client
package 
{
	import flash.display.Sprite;
	import uifactory.*;
	import flash.events.MouseEvent;
 
	public class SamBitClient extends Sprite
	{
		private var bitMapSam:BitMapCreator;
		private var bitMapSamNow:BitMapProduct;
		private var btnMk:UICreator;
		private var fileType:String;
		private var fileNow:String;
		private var btnG:Sprite;
		private var btnP:Sprite;
		private var btnJ:Sprite;
 
		public function SamBitClient()
		{
			btnMk = new SpriteButtonMaker  ;
			//(for label,x,y,color,width,height)
			btnG = btnMk.factoryMethod("Bitmap GIF",10,10,0x456173,100,20);
			btnG.addEventListener(MouseEvent.CLICK,chooseSam);
			addChild(btnG);
			btnP = btnMk.factoryMethod("Bitmap JPG",10,35,0x456173,100,20);
			btnP.addEventListener(MouseEvent.CLICK,chooseSam);
			addChild(btnP);
			btnJ = btnMk.factoryMethod("Bitmap PNG",10,60,0x456173,100,20);
			btnJ.addEventListener(MouseEvent.CLICK,chooseSam);
			addChild(btnJ);
		}
 
		private function chooseSam(e:MouseEvent)
		{
			if (bitMapSamNow)
			{
				removeChild(bitMapSamNow);
			}
 
			fileType = e.target.text;
			switch (fileType)
			{
				case "Bitmap GIF" :
					fileNow = "samGif.gif";
					break;
 
				case "Bitmap JPG" :
					fileNow = "samJpg.jpg";
					break;
 
				case "Bitmap PNG" :
					fileNow = "samPng.png";
 
			}
 
			bitMapSam = new BitMapSamMaker  ;
			bitMapSamNow = bitMapSam.factoryMethod(fileNow);
			addChild(bitMapSamNow);
			bitMapSamNow.x = 130,bitMapSamNow.y = 20;
		}
	}
}

With all of that code, the whole operation in the Client is only the following two lines:

bitMapSam = new BitMapSamMaker;
bitMapSamNow = bitMapSam.factoryMethod(fileNow);

The rest is UI. The UIFactory is the one used in the previous post with minor modifications in color and font. (It is included in the download.)

More On Bitmaps

I’d like to follow up this post with another on ActionScript 3.0 Bitmap objects. However, instead of creating Bitmaps from files, I’d like to explore creating them using the BitmapData object and some of the other Bitmap-related classes in ActionScript 3.0. As always, we’re interested in your feedback.

Share
Categories: Factory Method
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>