Java Reference
In-Depth Information
CHAPTER 11
Graphics
Java 1.0 and 1.1 provided rudimentary graphics capabilities through the
java.awt.Graphics
class and associated classes, such as
java.awt.Color
and
java.awt.Font
. In Java 1.2 and later, the Java 2D API provides state-of-the-art,
two-dimensional graphics facilities using the
java.awt.Graphics2D
class (a sub-
class of
Graphics
) and associated classes, such as
java.awt.BasicStroke
,
java.awt.GradientPaint
,
java.awt.TexturePaint
,
java.awt.AffineTransform
,
and
java.awt.AlphaComposite
.
This chapter demonstrates using all these classes; it shows how you can draw
graphics with and without the Java 2D API. The key class for all graphics opera-
tions is
Graphics
(or in the Java 2D API, its subclass,
Graphics2D
). The purpose of
this class is threefold:
It deÞnes the drawing surface
A
Graphics
object can represent the on-screen drawing area within an AWT
(or Swing) component. It can also represent an off-screen image you can
draw into or a piece of paper to be printed on a printer.
It deÞnes drawing methods
All primitive graphics operations, such as drawing lines, filling shapes, and
displaying text, are performed using methods of the
Graphics
class.
It deÞnes attributes used by the drawing methods
Various methods of the
Graphics
class can set the font, color, clipping region,
and other attributes used when performing graphics operations, such as draw-
ing lines, filling shapes, and displaying text. The values of these graphical
attributes are often instances of AWT classes, such as
Color
,
Font
,
Stroke
,
AffineTransform
, and so on.
The graphics capabilities of Java are intimately wed with the Abstract Windowing
Toolkit (AWT). As such, the
Graphics
and
Graphics2D
classes are part of the
java.awt
package, as are all the associated classes that define graphical attributes.
As we discussed in Chapter 10,
Graphical User Interfaces
, the central class of the
AW T i s
java.awt.Component
.
Component
is the basic building block of all graphical
user interfaces in Java. A
Graphics
object represents a drawing surface, but Java