Java Reference
In-Depth Information
Example 11•4: BouncingCircle.java (continued)
public void stop() {
// Set the flag that causes the run() method to end
pleaseStop = true;
}
}
TheJava2DAPI
We now turn to the Java 2D API, which is available in Java 1.2 and later. Features
of this API include:
•
The
Graphics2D
class, which is a subclass of
Graphics
that defines additional
graphics primitives and attributes.
•
The
Shape
interface, which Java 2D uses to define many graphics primitives
and other operations. The
java.awt.geom
package contains a number of use-
ful implementations of
Shape
.
•
The
Stroke
interface, which describes how lines are drawn (or stroked). The
BasicStroke
class implements this interface and supports the drawing of wide
and dashed lines.
•
The
Paint
interface, which describes how shapes are filled. The
Gradient-
Paint
implementation allows filling with color gradients, while
TexturePaint
supports tiling with images. Also, as of Java 1.2, the
Color
class implements
the
Paint
interface, to allow shapes to be filled with a solid color.
•
The
Composite
interface, which defines how colors being drawn are com-
bined with the colors already on the drawing surface. The
AlphaComposite
class allows colors to be combined based on the rules of alpha transparency.
The
Color
class also has new constructors and methods that support translu-
cent colors.
•
The
RenderingHints
class, which allows an application to request particular
types of drawing, such as antialiased drawing. Antialiasing uses transparent
colors and compositing to smooth the edges of text and other shapes, pre-
venting “jaggies.”
•
The
java.awt.geom.AffineTransform
class, which defines coordinate-system
transformations and allows shapes (and entire coordinate systems) to be trans-
lated, scaled, rotated, and sheared.
Because
AffineTransform
allows coordinate-system transformations, including
scaling, the Java 2D API distinguishes between the coordinate system of the
device (i.e., device space) and the (possibly) transformed user coordinate sys-
tem (i.e., user space). Coordinates in user space can be specified using
float
and
double
values, and graphics drawn using Java 2D are resolution-indepen-
dent.
In addition, in Java 1.2 and later, your applications are no longer restricted to
using a fixed set of logical fonts; you can now use any font installed on the sys-
tem. Finally, the Java 2D API includes an easy-to-use image-processing API.