Chapter 20. Drawing with the 2D API
In previous chapters, we looked at the components and component architecture of graphical user interfaces (GUIs) using Java’s Swing and AWT packages. Now, we’re going to drop down a level and look at the procedural APIs for rendering graphics in Java applications. These APIs are used by components to paint themselves on the screen and to display icons and graphics. This chapter goes into some detail about Java’s sophisticated 2D, resolution-independent drawing API and the core tools for loading and displaying images. In the next chapter, we’ll explore dynamic image-processing tools in more detail and look at the classes that let you generate and modify image data, pixel by pixel.
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
and cover the drawing of shapes, text, and images.
Figure 20-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).
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 ...
Get Learning Java, 3rd Edition 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.