Beginning Java 2- JDK 1.3 Version
Images and Animation
Implementing imageUpdate()
Most of the time, the default implementation of
imageUpdate() is sufficient. it just repaints the component. If we use the getImage()
method in the previous example to load the image, we have two objects that are
interested in the image data, the applet object and the ImagePanel
object. For the ImagePanel
object, the default imageUpdate()
method is fine. The applet object is different. It wants to know the height and
the width of the image so it can resize itself to fit the image. Calling getWidth()
and getHeight()
for the Image
object provide these values if they are available, but these may not be
available during the execution of the init()
method even when the image file is local, in which case these methods will
return . 1. In this case therefore, we can usefully implement our own version of
the >imageUpdate()
method to do what we want.
There are six parameters to the imageUpdate() method and it returns a boolean value so its outline implementation is like this:
public boolean imageUpdate(Image img, // Reference to the image
int flags, // Flags identifying what is available
int x, // x coordinate
int y, // y coordinate
int width, // Image Width
int height) // Image height
{
// Code to respond to data that is available..
}
The imageUpdate()
method will be called when any new item of image data becomes available, so the
data will typically be incomplete. You might have the width of the image
available for instance but not the height. A return value of false indicates that you have received all the image data that you need, otherwise the method should return true so it will be called again later when more information is available.
The reference passed as the first parameter identifies the image that the call relates to, so if your class object is observing more than one image, you can use this to tell to which Image object the call of the method relates. The second parameter provides the key to determining what image data is available. The flags value is a combination of one or more of the following single bit flags:
|
Parameter |
Description |
|
WIDTH |
The width of the image is available. |
|
HEIGHT |
The height of the image is available. |
|
SOMEBITS |
Some more pixels required for a scaled version of the
image are available, and the bounding rectangle for these is given by the x,
y, width and height values. |
|
ALLBITS |
This flag indicates that all the bits of an image that
has been drawn previously are now available. This enables the updateImage()
method to decide when to return false
when an image is being drawn incrementally. |
|
FRAMEBITS |
This indicates that another complete frame of a
multi-frame image that has been previously drawn is now available to be drawn
again. This can be used to determine when to call the repaint()
method to display more of an image. |
|
PROPERTIES |
This flag indicates that the properties of the image are
now available. These are obtained by calling the getProperties()
method for the Image
object. |
|
ABORT |
This flag indicates that the process of retrieving the
image was aborted and no further image data will be available. |
|
ERROR |
This flag indicates that an error has occurred retrieving
the image and no further image data will be
available. |
We aren't going to go into the detail of how all these flags
are used, but the complete set is here for reference. We are only interested in
the height and the width of the image in our applet, so we can implement the
imageUpdate() method as:
public boolean imageUpdate(Image img, // Reference to the image
int flags, // Flags identifying what is available
int x, // x coordinate
int y, // y coordinate
int width, // Image width
int height) // Image height
{
if((flags & WIDTH) > 0 && (flags & HEIGHT) > 0)
{
resize(width,height); // Set applet size to fit the image
repaint(); // Repaint the applet in its new size
return false;
}
else
return true; // More info required
}
We only want to resize the applet when we have both the
width and height of the image, so we test for each of these flags by bitwise
ANDing the flags value with the WIDTH and HEIGHT values that are defined in the
ImageObserver interface. If the results of both operations are positive, both
flags were set so we can resize the applet and repaint it. While either or both
of the WIDTH and HEIGHT flags are not set, we return true so the method will be
called again. Of course, the HTML rendering in a browser will work much better
if the correct size for the applet is specified at the outset.
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.