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.
In a typical Swing application, the simplest way to display an image in
your application is to use an ImageIcon
with a JLabel component. Here, we are
talking about working with image data at a lower level, for painting. The
2D API has a powerful set of tools for generating and displaying image
data. We’ll start with the basics of the java.awt.Image class and see how to load an
image into an application and draw it where you want it. The Java AWT
toolkit will handle most of the details for us. In the next chapter, we’ll
go further to discuss how to manage image loading manually as well as how
to create and manipulate raw pixel data, allowing you to create any kind
of graphics you can dream up.
The core AWT supports images encoded in JPEG, PNG, and GIF. (This
includes GIF89a animations so that you can work with simple animations as
easily as static images.) If you need to work with other types of images,
you can turn to the Java Advanced Imaging javax.imageio framework. We’ll mention it
briefly here and again in the next
chapter when we discuss the BufferedImage class.
In many ways, the ImageIO framework supercedes and replaces the older image handling functionality of the core AWT just as Swing extends and replaces the old AWT components. The ImageIO framework is easily extensible for new image types through plug-ins. However, out ...