Java Reference
In-Depth Information
caninvoke
Graphics2D
's
void transform(AffineTransform Tx)
method
to concatenate
Tx
to the existing transformation matrix.
Tip
It'sagoodideatouse
transform()
insteadof
setTransform()
because
the
Graphics2D
instancepassedtoacomponent's
paint()
methodissetupwith
voking
setTransform()
maychangethisorganizationandleadtoconfusingresults
unless you know what you're doing.
Forcommontransformations,
Graphics2D
declaresmethodssuchas
void ro-
tate(double theta)
,
void scale(double sx, double sy)
,and
void
translate(double tx, double ty)
. These methods offer a convenient al-
ternative to instantiating
AffineTransform
and passing this instance to
trans-
form()
.
Caution
Graphics2D
declaresa
void translate(int x, int y)
meth-
od for translating the origin of the
Graphics2D
context to the point (
x
,
y
) in the
currentcoordinatesystem.Thismethodisinvokedinsteadof
translate(double,
double)
whenyoupassintegerarguments,sobecarefulwhenpassingargumentsor
you might end up with unexpected results.
Figure7-21
showsuntransformed(blue),rotated(gradientgreentored),andsheared
(gradient green to red with almost no green) rectangles.