Java Reference
In-Depth Information
The endcap- and juncture-style arguments use
BasicStroke
class variables.
Endcap
styles apply to the ends of lines that do not connect to other lines.
Juncture
styles apply
to the ends of lines that join other lines.
Possible endcap styles are
CAP_BUTT
for no endpoints,
CAP_ROUND
for circles around each
endpoint, and
CAP_SQUARE
for squares. Figure 13.5 shows each endcap style. As you can
see, the only visible difference between the
CAP_BUTT
and
CAP_SQUARE
styles is that
CAP_
SQUARE
is longer because of the added square endcap.
FIGURE 13.5
Endpoint cap
styles.
CAP_BUTT
CAP_ROUND
CAP_SQUARE
Possible juncture styles include
JOIN_MITER
, which joins segments by extending their
outer edges,
JOIN_ROUND
, which rounds off a corner between two segments, and
JOIN_
BEVEL
, which joins segments with a straight line. Figure 13.6 shows examples of each
juncture style.
FIGURE 13.6
Endpoint juncture
styles.
JOIN_MITER
JOIN_ROUND
JOIN_BEVEL
The following statements create a
BasicStroke
object and make it the current stroke:
BasicStroke pen = BasicStroke(2.0f,
BasicStroke.CAP_BUTT,
BasicStroke.JOIN_ROUND);
comp2D.setStroke(pen);
The stroke has a width of 2 pixels, plain endpoints, and rounded segment corners.
13
Creating Objects to Draw
After you have created a
Graphics2D
object and specified the rendering attributes, the
final two steps are to create the object and draw it.
A drawn object in Java2D is created by defining it as a geometric shape using a class in
the
java.awt.geom
package. You can draw lines, rectangles, ellipses, arcs, and polygons.
The
Graphics2D
class does not have different methods for each of the shapes you can
draw. Instead, you define the shape and use it as an argument to
draw()
or
fill()
methods.