758 Part II: The Java Library
When this applet runs, it starts loadingimgin theinit( )method. Onscreen you can see
the image as it loads from the network, becauseApplet’s implementation of theImageObserver
interface callspaint( )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.
ImageObserver
ImageObserveris 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.
TheimageUpdate( )method has this general form:
boolean imageUpdate(ImageimgObj, intflags, intleft, inttop, intwidth, intheight)
Here,imgObjis the image being loaded, andflagsis an integer that communicates the status
of the update report. The four integersleft, top, width,andheightrepresent a rectangle that
contains different values depending on the values passed inflags.imageUpdate( )should
returnfalseif it has completed loading, andtrueif there is more image to process.
Theflagsparameter contains one or more bit flags defined as static variables inside the
ImageObserverinterface. These flags and the information they provide are listed in Table 25-1.
TheAppletclass has an implementation of theimageUpdate( )method for the
ImageObserverinterface that is used to repaint images as they are loaded. You can
override this method in your class to change that behavior.
FIGURE 25-1
Sample
output from
SimpleImageLoad