Skip to Content
J2ME in a Nutshell
book

J2ME in a Nutshell

by Kim Topley
March 2002
Intermediate to advanced
480 pages
18h 40m
English
O'Reilly Media, Inc.
Content preview from J2ME in a Nutshell

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.

Translating the Graphics 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, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Enterprise J2ME™: Developing Mobile Java™ Applications

Enterprise J2ME™: Developing Mobile Java™ Applications

Michael Juntao Yuan
Java™ Performance

Java™ Performance

Charlie Hunt, Binu John

Publisher Resources

ISBN: 059600253XCatalog PageErrata