Displaying Images
So far, we’ve worked
with methods for drawing simple shapes
and displaying text. For more complex graphics, we’ll be
working with images. The 2D API has a powerful set of tools for
generating and displaying image data. These tools address the
problems of working in a distributed and multithreaded application
environment. We’ll start with the basics of the
java.awt.Image class and see how to get an image
into an applet or application and draw it on a display. This job
isn’t quite as simple as it sounds; the browser might have to
retrieve the image from a networked source when we ask for it.
Fortunately, if we’re just interested in getting the image on
the screen whenever it’s ready, we can let the graphics system
handle the details for us. In the next chapter, we’ll discuss
how to manage image loading manually, as well as how to create raw
image data and feed it efficiently to the rest of an application.
The Image Class
The java.awt.Image
class represents a view of an image.
The view is created from an image source that produces pixel data.
Images can be from a static source, such a
GIF, JPEG, or PNG data file, or a dynamic one, such as a video stream
or a graphics engine. The Image class in Java 2
also handles
GIF89a animations, so that you can work
with simple animations as easily as static images.
An applet can ask its viewer to
retrieve an image by calling the getImage( )
method. The location of the image to be retrieved is given as a
URL, either absolute ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access