Waiting for an Image to Load

Both the applet and application presented so far load and display their images in chunks, from the top of the image to the bottom, due to the asynchronous nature of image loading. A more aesthetically pleasing approach is to wait for the image to load completely before displaying it. There are a number of ways to accomplish this approach; one way is to implement imageUpdate(), which keeps us informed as to the progress of the loading of the image in question.

The applet in Example 5-4 is almost exactly like the one in Example 5-1, except that it implements ImageObserver.imageUpdate().

Example 5-4. An Applet that Waits for An Image to Fully Load
 import java.applet.Applet; import java.awt.Graphics; import java.awt.Image; ...

Get Graphic Java™ 1.2, Volume I: AWT, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.