9.1. Overview

An image is a two-dimensional array of colors. Each element in the array is called a pixel. Although this sounds like a description of a drawing surface, the two concepts are distinct. The pixels of the image do not necessarily correspond directly to the pixels of a drawing surface. The image has a width and height, measured in pixels, and a coordinate system that is independent of any drawing surface.

9.1.1. java.awt.Image

The java.awt.Image class is the center of Java's image universe. It represents a rectangle of colored pixels. The image data is not directly accessible. In essence, then, an Image is just a box for image data. You can retrieve the width and height of the image, if they are available:

public abstract int getWidth(ImageObserver observer)

This method returns the width of the Image, measured in pixels. If the width is not available (because the image data has not yet been loaded), -1 is returned. The supplied ImageObserver will be notified as image data becomes available. You can pass null for the ImageObserver parameter if you wish. (I'll talk more about ImageObservers soon.)

public abstract int getHeight(ImageObserver observer)

This method returns the height of the Image, measured in pixels. It behaves just like getWidth().

The Graphics2D class knows how to render an Image to a drawing surface, using one of its many drawImage() methods. As an image makes its way through the rendering pipeline, it is transformed, clipped, and composited, just ...

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.