Recipe 8.6.
Creating Rectangular Fills
Problem
You want
to fill a rectangular area of a bitmap with a color.
Solution
Use the fillRect( ) method of the
BitmapData class.
Discussion
The BitmapData class does not have the
wealth of drawing tools available in the drawing API for movie
clips and sprites. It does have a basic rectangle fill tool,
though. Using this method is quite simple. Just pass in the
rectangle area you want to fill and the color value with which you
want to fill it:
_bitmap.fillRect(rectangle, color);
The rectangle must be an instance of the
flash.geom.Rectangle
class. You can make an instance of the class by calling its
constructor with the x, y, width, and height of the
rectangle you want to create, as so:
var rect:Rectangle = new Rectangle(0, 0, 50, 100);
The following code creates a white bitmap, and
then draws a red square in the middle of it:
public function RectExample( ) {
_bitmap = new BitmapData(100, 100, false, 0xffffffff);
var image:Bitmap = new Bitmap(_bitmap);
addChild(image);
_bitmap.fillRect(new Rectangle(25, 25, 50, 50), 0xffff0000);
}
Note that there is only one version of
fillRect( ) for both transparent and non-transparent images.
If you are using it with an image that does not have an alpha
channel, just pass a 24-bit color value, as the extra bits for
alpha are ignored anyway. If you are using it with a transparent
image, be sure to use a full 32-bit number. If you just pass a
24-bit value, the alpha channel is set at 0 percent alpha.
See Also
Recipes 8.5,
8.7,
8.8,
8.9,
8.10,
and 8.11
for other ways to add graphical content to a bitmap.
|