Java Reference
In-Depth Information
display the series of connected lines specified with the arguments
xValues2
,
yValues2
and
7
(the number of points).
Lines 27-28 create two
int
arrays and use them to specify the points of a polygon.
Array
xValues3
contains the
x
-coordinate of each point and array
yValues3
the
y
-coordi-
nate of each point. Line 29 displays a polygon by passing to
Graphics
method
fill-
Polygon
the two arrays (
xValues3
and
yValues3
) and the number of points to draw (
4
).
Common Programming Error 13.1
An
ArrayIndexOutOfBoundsException
is thrown if the number of points specified in the
third argument to method
drawPolygon
or method
fillPolygon
is greater than the num-
ber of elements in the arrays of coordinates that specify the polygon to display.
Line 32 creates
Polygon
polygon2
with no points. Lines 33-37 use
Polygon
method
addPoint
to add pairs of
x-
and
y
-coordinates to the
Polygon
. Line 38 displays
Polygon
polygon2
by passing it to
Graphics
method
fillPolygon
.
The
Java 2D API
provides advanced two-dimensional graphics capabilities for program-
mers who require detailed and complex graphical manipulations. The API includes features
for processing line art, text and images in packages
java.awt
,
java.awt.image
,
ja-
va.awt.color
,
java.awt.font
,
java.awt.geom
,
java.awt.print
and
java.awt.im-
age.renderable
. The capabilities of the API are far too broad to cover in this textbook. For
this section, we overview several Java 2D capabilities.
Drawing with the Java 2D API is accomplished with a
Graphics2D
reference (package
java.awt
).
Graphics2D
is an
abstract subclass
of class
Graphics
, so it has all the graphics
capabilities demonstrated earlier in this chapter. In fact, the actual object used to draw in
every
paintComponent
method is an instance of a
subclass
of
Graphics2D
that is passed to
method
paintComponent
and accessed via the
superclass
Graphics
. To access
Graphics2D
capabilities, we must cast the
Graphics
reference (
g
) passed to
paintComponent
into a
Graphics2D
reference with a statement such as
Graphics2D g2d = (Graphics2D) g;
The next two examples use this technique.
Lines, Rectangles, Round Rectangles, Arcs and Ellipses
This example demonstrates several Java 2D shapes from package
java.awt.geom
, includ-
ing
Line2D.Double
,
Rectangle2D.Double
,
RoundRectangle2D.Double
,
Arc2D.Double
and
Ellipse2D.Double
. Note the syntax of each class name. Each class represents a shape
with dimensions specified as
double
values. There's a
separate
version of each represented
with
float
values (e.g.,
Ellipse2D.Float
). In each case,
Double
is a
public
static
nested
class of the class specified to the left of the dot (e.g.,
Ellipse2D
). To use the
static
nested
class, we simply qualify its name with the outer-class name.
In Figs. 13.29-13.30, we draw Java 2D shapes and modify their drawing characteris-
tics, such as changing line thickness, filling shapes with patterns and drawing dashed lines.
These are just a few of the many capabilities provided by Java 2D.