Java Reference
In-Depth Information
When
draw()
is called,
Graphics2D
uses the object passed to
setStroke()
tofigureoutwhattheoutlinelookslikeandusestheobjectpassedto
setPaint()
to paint the outline's pixels.
Theonlyclassthatimplementsthisinterfaceis
java.awt.BasicStroke
,which
letsyoudefineashapeoutlineintermsofapenwidth(measuredperpendicularlytothe
pen's trajectory), end caps, join styles, miter limit, and dash attributes.
A shape's outline is infinitely thin and is drawn with a pen that has a certain width,
whichisexpressedasafloating-pointvalue.Theresultingoutlineshapeextendsbeyond
this outline and into the shape's interior.
Linesegmentscanbedrawnwithorwithoutdecorationsatbothends.Thesedecora-
tionsareknownas
end caps
.
BasicStroke
declares
CAP_BUTT
,
CAP_ROUND
,and
CAP_SQUARE
constantstoindicatethatnoendcapsarepresent,thatasemicirclewith
a radius equal to half of the pen width appears at both ends, or that a rectangle with a
length equal to half of the pen width appears at both ends.
Whentwolinesegmentsmeet,
Graphics2D
usesa
join style
tojointhemtogether
so that they don't present a ragged edge.
BasicStroke
declares
JOIN_BEVEL
,
JOIN_MITER
,and
JOIN_ROUND
constantstoindicate
beveled
(squaredoff),
mitered
(sharpened to triangular points), or rounded joins. When the miter exceeds a specified
miter limit, the join is beveled.
Finally,
BasicStroke
letsyouspecifydashedlinesbyprovidingadasharrayand
adashphasevalue.The
dash array
containsfloating-pointvaluesrepresentingtheuser
space lengths of visible and invisible sections of line segments. Even-indexed array
elements determine lengths of visible sections; odd-indexed array elements determine
lengths of invisible sections.
Forexample,consideradasharrayof[8.0,6.0].Thisarray'sfirst(even)elementin-
dicatesthatvisiblelinesegmentsare8.0unitslong,anditssecond(odd)elementindic-
atesthatinvisiblelinesegmentsare6.0unitslong.Youendupwithapatternof8visible
units, 6 invisible units, 8 visible units, 6 invisible units, and so on.
The
dash phase
is a floating-point offset into the dash pattern specified by the dash
array; it is not an offset into the array. When the dash phase is 0, the line segment is
stroked as indicated in the previous example. However, when a nonzero dash phase is
specified,thefirstlinesegmentbeginsdashphaseunitsfromthevalueprovidedbythe
first array entry.
For example, given the previous array, suppose you specified a dash phase of 3.0.
Thisvalueindicatesthatthefirstvisiblelinesegmentis8-3or5unitslong,andisfol-
lowed by 6 invisible units, 8 visible units, 6 invisible units, and so on.