Images and Active Objects - putting it all togetherTopExplaining Drawable Images

Drawable Images

Demo. Drag a Snowman

In the above demo program, we drag a picture of a snowman around the screen. The picture comes from a "gif" file named snowman.gif.

The first line of the begin method of the Snow class shows how to do this when given a "gif" file (a particular format for holding images on-line):

    snowManPic = getImage("snowman.gif");

where snowManPic is an instance variable declared to have type Image. Downloading a "gif" file can often be slow, so we usually will want to create an image from the "gif" file at the beginning of a program and save it until we need it. If you download "gif" files in the middle of your program, you may cause a long delay while the computer brings it in from a file on a local disk or fileserver.

The class VisibleImage from our objectdraw library will allow you to treat an image roughly as you would a rectangle. In fact, imagine a VisibleImage to be a rectangle with a picture embedded in it. You can do everything you would with a rectangle, except that there's a neat picture on top.

To create a new VisibleImage:

    new VisibleImage( anImage, xLocation, yLocation, canvas);

For example, new VisibleImage(snowManPic, 10, 10, canvas); would create an object of type VisibleImage from the image in snowManPic and place it on canvas at location (10,10).

If you want to manipulate that VisibleImage in interesting ways, you'll need to associate a name with it as usual:

    VisibleImage snowMan;

And then later:

    snowMan = new VisibleImage(snowManPic, 10, 10, canvas);

You can then invoke all the standard rectangle methods on the VisibleImage. For example:

    snowMan.setWidth(100);
    snowMan.setHeight(100);

Images and Active Objects - putting it all togetherTopExplaining Drawable Images