<< Chapter < Page Chapter >> Page >

The two main bitmap classes

The two main ActionScript classes that are used with bitmaps are:

What is the difference between the two classes?

Briefly, an object of the BitmapData class encapsulates the actual pixel color and transparency values that represent an image. An object ofthe Bitmap class is a subclass of the DisplayObject class, meaning that it can be placed on the display list. A Bitmap object encapsulates a BitmapData object.

A 32-bit ARGB color value: I will have a lot more to say about pixels, color values, etc. in future lessons. My objective for this lesson issimply to get you to the point that you can create and display bitmap data.

A new object of the BitmapData class

The constructor for the Bitmap03 class begins in Listing 1. The constructor instantiates a new object of the BitmapData class with a red opaque background. The constructor parameters for a BitmapData object are:

  • width :int -- The width of the bitmap image in pixels. The width is specified as 100 pixels in Listing 1.
  • height :int -- The height of the bitmap image in pixels. The height is specified as 100 pixels in Listing 1.
  • transparent -- Boolean (default = true) -- Specifies whether the bitmap image supports per-pixel transparency. The default value is true (transparent). Avalue of false (opaque) is specified in Listing 1.
  • fillColor :uint (default = 0xFFFFFFFF -- A 32-bit ARGB color value that you use to fill the bitmap image area. The color red is specified in Listing 1.

Draw a yellow cross on the background

A BitmapData object encapsulates a rectangular array of pixel data. The class provides several methods that make it possible for you to get and set individual pixel values in terms of color and transparency.

One of those methods is the method named setPixel that lets you replace (or merge with transparency) an existing pixel with a new pixel on the basis of the horizontal and vertical coordinates of the pixel.

Listing 2 uses a for loop to replace some of the red pixels in the BitmapData object with yellow pixels to form a yellow cross as shown in Figure 1.

Draw a yellow cross on the background.

var yellow:uint = 0xFFFF00; for(var cnt:uint = 0; cnt<100; cnt++){ bitmapData.setPixel(cnt, cnt, yellow);bitmapData.setPixel(100 - cnt, cnt, yellow); } //end for loop

The setPixel method

The setPixel method requires three parameters. The first two are the horizontal and vertical coordinates of the pixel whose colorvalues are to be set. The third is an unsigned integer value that specifies the color that will be assigned to the pixel at that location.

Without getting into the details at this point, suffice it to say that the value assigned to the yellow variable in Listing 2 specifies the visible color of yellow.

Encapsulate the BitmapData object in a Bitmap object

The constructor for the Bitmap class has three parameters, but they all have default values. The first parameter, if provided, must be areference to an object of type BitmapData .

Continuing with the constructor for the Bitmap03 class, Listing 3 instantiates a new Bitmap object passing the BitmapData object prepared in Listing 2 as a parameter.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with actionscript. OpenStax CNX. Jun 04, 2010 Download for free at http://cnx.org/content/col11202/1.19
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with actionscript' conversation and receive update notifications?

Ask