Reviews : Java Books :
Beginning Java 2- JDK 1.3 Version : Images and Animation

Buy this book
Title: Beginning Java 2- JDK 1.3 Version
ISBN: 1861003668
US Price: $ 49.99
Canadian Price:
C$ 74.95
UK Price: £ 35.99
Publication Date: March 2000
Pages: 1230
© Wrox Press Limited, US and UK.

Beginning Java 2- JDK 1.3 Version
Images and Animation

Creating Image Objects

The Image class is at the heart of all images in Java. An image will always be an object of a class that has Image as a base. You can't directly create an Image object that contains an image because the class is abstract. While a reference of type Image is used to refer to an object that encapsulates the data for an image, the Image class does not provide you with the means of obtaining the image data. There has to be some other mechanism for creating an Image object corresponding to a particular image.

The Applet class defines two overloaded versions of a method, getImage(), that return a reference to an Image object. One version accepts an object of type URL as an argument that specifies the source of the image data. The other accepts two arguments, a URL object plus a String object, where the String object defines the specification of the source of the image data in the context of the URL object. In the previous example, we could have created an Image object directly with the following statement:


 Image image = getImage(new URL(getCodeBase(),"Images/wrox_logo.gif"));

We still need to put this in a try block because of the possibility of a MalformedURLException (or others) being thrown by the URL class constructor. There is a significant difference between this statement obtaining a reference to an Image object for an image from a given source, and the statement in the example that created an ImageIcon object. When the ImageIcon class constructor returned to the init() method, the object was fully constructed and the image was available. In the statement above we get a reference to an Image object returned that may at some time in the future contain the data for the image. This gets over the problem implicit in using an ImageIcon object . it hangs up your applet until all the image data for the icon has been retrieved from the source. When you call getImage(), your applet code can continue to execute and get on with other things while the image data is being downloaded. This still leaves the question of determining when the image is actually here which is where the ImageObserver interface comes in.

Image Observers

The Component class implements the ImageObserver interface so all components will inherit this implementation. The ImageObserver interface declares one method, imageUpdate(), that is called automatically when information about an image that was previously requested becomes available. The imageUpdate() method will draw the image on the component. So how do you request information about an image?

One way is to call the drawImage() method for a Graphics object. By calling this method you implicitly request all the information necessary to draw the image in the graphics context . the width, the height, the pixel data and so on. As you saw, the last argument to the drawImage() method was a reference to an ImageObserver object that in our case was the panel on which the image was to be drawn. If any of the image data was not available at the time of the call, the imageUpdate() method for the ImageObserver object would be called as more information became available.

Another way you can request information about an image is to call methods for the Image object itself. The Image class defines methods getWidth() and getHeight() to access the width and height of the image. Both of these methods require a reference of type ImageObserver to be passed . normally a reference to the component on which the image is to be drawn . and that object's imageUpdate() method is called when the information becomes available.

The last part we need to understand in all this is exactly what the imageUpdate() method does when it is called. It simply draws the image incrementally with the information available. This has the effect on a slow Internet link or with a large image of making the image appear piecemeal, as and when the data becomes available. This usually gives the user a visual cue that, even though the entire image has not been retrieved yet, something is still happening.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.