<< Chapter < Page Chapter >> Page >

Dealing with a type compatibility issue

In the earlier lesson titled Bitmap Basics , I explained that in order to add a child to a VBox object, that child:

  • Must be a subclass of the DisplayObject class and
  • Must implement the IUIComponent interface.

While a Bitmap object is a subclass of DisplayObject , it does not implement the IUIComponent interface. Therefore, it is not compatible with being added directly to the VBox object. I resolved the issue in that lesson by encapsulating the Bitmap object in an object of the UIComponent class, which implements the IUIComponent interface.

Encapsulate the Bitmap in an Image object

In this lesson, I decided to be more specific and encapsulate the Bitmap object in an object of the Image class. This is allowable because the Image class is a subclass of the UIComponent class.

Listing 5 encapsulates the Bitmap in an Image object and adds it to the VBox object to be displayed at the top of and five pixels to the right of the left edge of the VBox as shown by the top image in Figure 1.

Encapsulate the bitmap in an image object.

//Encapsulate the bitmap in an Image object and add // the Image object to the VBox. Display it at// x=5 and y=0 original.x = 5;original.y = 0; var imageA:Image = new Image();imageA.addChild(original); this.addChild(imageA);

A curious situation

This brings up a curious situation regarding the placement of the Image objects in the VBox object. Normally, if you instantiate Image objects and populate them directly from the contents of image files ( by calling the load method on the Image object) , you can add them to a VBox object without the requirement to specify the locations at which the images will be displayed. The layout management rules of the VBox object determine how they are displayed.

This case is different

In this case, however, if you instantiate Image objects and populate them with Bitmap objects by calling the addChild method as shown in Listing 5, you must specify the display locations of the Bitmap objects within the VBox object. If you don't, they all end up in the upper-left corner of the VBox .

Honoring the boundaries of the VBox object

Also, if you specify the dimensions of the VBox object and add more images of the first type than the size of the VBox object can accommodate, scroll bars automatically appear on the VBox object.

In this case, however, if you specify the locations such that the Image objects won't all fit within the boundaries of the VBox object, the images extend outside the bounds of the VBox object.

I will leave it as an exercise for the student to sort through all of that.

Clone the original Bitmap to create a duplicate Bitmap

We have now reached the point where we could access the BitmapData object encapsulated in the Bitmap object and modify the pixel data that comprises the image. However, instead of modifying the pixels in the original Bitmap , I elected to create a duplicate bitmap and modify the pixels in the duplicate. That makes it possible to compare the unmodified image (top image in Figure 1) with the modified image (middle image in Figure 1).

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