9.3. Displaying Images
Images are rendered on a drawing surface using drawImage() in the Graphics and Graphics2D classes. This method comes in many flavors, as you'll see. To understand how drawImage() works, you first need to understand the concept of an image observer. This concept has been in the AWT since Java 1.0, so I'll just review it briefly.[1]
[1] For more information, see John Zukowski's Java AWT Reference (O'Reilly) or Exploring Java, by Pat Niemeyer and Josh Peck (O'Reilly).
9.3.1. ImageObserver
The Java platform was designed to be savvy about networks. For images, this boils down to the premise that it could take a significant amount of time to load an image over the network. The drawImage() methods in Graphics and Graphics2D, therefore, support the concept of an image observer, an object which monitors the progress of loading image data. When you call drawImage() to place an image on a component, it draws as much of the image as is available and returns. If none of the image data is available, the method returns immediately. In a separate thread, however, the image data is loading. The drawImage() method accepts an image observer parameter. As image data is loaded, the image observer is notified. Typically the image observer will repaint() the component, which calls drawImage() again. Since the image data is loaded, the image will be painted immediately.
In the ShowImage applet, earlier in this chapter, this is passed to drawImage() as the ImageObserver. How can ...
Get Java 2D Graphics 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.