img
FIGURE 25-1
Sample
output from
SimpleImageLoad
When this applet runs, it starts loading img in the init( ) method. Onscreen you can see
the image as it loads from the network, because Applet's implementation of the ImageObserver
interface calls paint( ) every time more image data arrives.
Seeing the image load is somewhat informative, but it might be better if you use the
time it takes to load the image to do other things in parallel. That way, the fully formed
image can simply appear on the screen in an instant, once it is fully loaded. You can use
ImageObserver, described next, to monitor loading an image while you paint the screen
with other information.
ImageObser ver
ImageObserver is an interface used to receive notification as an image is being generated, and
it defines only one method: imageUpdate( ). Using an image observer allows you to perform
other actions, such as show a progress indicator or an attract screen, as you are informed of
the progress of the download. This kind of notification is very useful when an image is being
loaded over the network, where the content designer rarely appreciates that people are often
trying to load applets over a slow modem.
The imageUpdate( ) method has this general form:
boolean imageUpdate(Image imgObj, int flags, int left, int top, int width, int height)
Here, imgObj is the image being loaded, and flags is an integer that communicates the status
of the update report. The four integers left, top, width, and height represent a rectangle that
contains different values depending on the values passed in flags. imageUpdate( ) should
return false if it has completed loading, and true if there is more image to process.
The flags parameter contains one or more bit flags defined as static variables inside the
ImageObserver interface. These flags and the information they provide are listed in Table 25-1.
The Applet class has an implementation of the imageUpdate( ) method for the
ImageObserver interface that is used to repaint images as they are loaded. You can
override this method in your class to change that behavior.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home