Chapter 17. Drawing with the 2D API
In the last few chapters, you’ve caught a glimpse of how graphics operations are performed in Java. This chapter goes into more depth about drawing techniques and the tools for working with images in Java. In the next chapter, we’ll explore image-processing tools in more detail, and we’ll look at the classes that let you generate images, pixel by pixel, on the fly.
The Big Picture
The classes you’ll use for
drawing come from six
packages:
java.awt, java.awt.color,
java.awt.font, java.awt.geom,
java.awt.image, and
java.awt.print. Collectively, these classes make
up most of the 2D API, a comprehensive API for drawing shapes, text,
and images. Figure 17.1 shows a
bird’s-eye view of these classes. There’s much more in
the 2D API than we can cover in two chapters. For a full treatment,
see Jonathan Knudsen’s Java 2D Graphics
(O’Reilly & Associates).

Figure 17-1. Graphics classes of the 2D API
An instance
of java.awt.Graphics2D is called a
graphics context. It represents a drawing
surface such as a component’s display area, a page on a
printer, or an offscreen image buffer. A graphics context provides
methods for drawing three kinds of graphics objects:
shapes, text, and images.
Graphics2D is called a graphics context because it also holds contextual information about the drawing area. This information includes the drawing area’s clipping region, ...
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