Translating the Graphics Origin
The origin of the Graphics
object that you get in
the paint( )
method is initially placed at the top
left of the Canvas
. However, you can move it to
any location you choose using the translate( )
method:
public void translate(int x, int y)
This method relocates the origin to the point (x
,
y
) as measured in the coordinates that apply
before this call is made. If the paint( )
method
begins with the following statements:
g.drawLine(0, 0, 20, 0); g.translate(10, 10); g.drawLine(0, 0, 20, 0);
a line is first drawn along the top of the Canvas
from (0, 0) to (20, 0), the origin is shifted so that (0, 0) is at
the point (10, 10) relative to the top left corner of the Canvas, and
finally another line is drawn. This line stretches from (0, 0) to
(20, 0) in the new coordinate system, which is
the same as (10, 10) to (30, 10) relative to the the
Canvas
itself. Figure 5-12
illustrates the effect of moving the origin.
Figure 5-12. Translating the Graphics origin
Once you have moved the origin, the effect of another
translate( )
call is cumulative with respect to
the first. This means that, for example, the following code results
in the origin being moved to (10, 10) and then back to its initial
location:
g.translate(10, 10); g.translater(-10, -10);
The following code moves to the origin to (15, 15) relative to the
top left-hand corner of the Canvas
:
g.translate(10, ...
Get J2ME in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.