11.1. BufferedImage

The java.awt.image.BufferedImage class is the centerpiece of the 2D API's new image capabilities. As a subclass of java.awt.Image, you can pass a BufferedImage to all of the drawImage() methods in Graphics and Graphics2D. What's so great about BufferedImage? You can access the data for the image directly, a task that was awkward previously. In this section, I'll give a quick overview of all the classes that make up a BufferedImage. Then I'll talk briefly about how you can use BufferedImage directly without worrying about what's inside it. In the remainder of this chapter, I'll burrow deeper and deeper into BufferedImage.

11.1.1. Inside BufferedImage

Remember, an image is a rectangular array of colored pixels. That's all it is. As we discuss the innards of BufferedImage, it may sound a lot more complicated. It will help to keep the simple picture in your mind.

Figure 11.1 shows what's inside a BufferedImage. I'll explain the relationships between all the pieces in this section. In the rest of this chapter, I'll explain each class — ColorModel, Raster, SampleModel, and DataBuffer — in detail.

Figure 11.1. BufferedImage from top to bottom

Every BufferedImage consists of a raster and a color model. The image data itself resides in the raster, while the color model knows how to interpret the data as color.

The raster contains the data that determines each pixel's ...

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.