translate() Sample program in J2ME

By Baski Viewed: 31732 times Emailed: 239 times Printed: 251 times Bookmark and Share



The point (x, y) specifies to a drawing function a location relative to the point (0, 0). The point (0, 0) is the origin of the Graphics. When you first obtain a reference to your canvas Graphics, its origin, the point (0, 0), always represents the top-left corner of the device's display (the destination).

To translate the origin of a Graphics means to reposition its origin. After translation, the origin of the Graphics represents some point other than the top-left pixel on the destination. You translate the origin of a Graphics using the method

void translate(int x, int y)

The arguments are the coordinates of the point that becomes the new origin of the Graphics object. The point (0, 0) now refers to this new origin. All drawing operations are now relative to this new origin. The code below simply draws a filled square on the Canvas.

Clicking the Go button translates the Graphics origin and then redraws the filled rectangle. The coordinates are always relative to the origin of the Graphics, not to the top-left corner of the device's display area. Drawing operations are always specified relative to the origin of the Graphics, regardless of the point on the destination it represents. Clicking the Go button actually toggles the translation. Clicking the button a second time translates the origin back to the top-left corner of the display.

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;

/**
Demonstrates the translation of a Graphics context on a
Canvas.
@see javax.microedition.lcdui.Graphics
*/
public class TranslationDemo extends Canvas
implements CommandListener
{
private final int WHITE =
0xFF << 16 | 0xFF << 8 | 0xFF;
private GraphicsDemo gDemo = GraphicsDemo.getInstance();
private Display display = Display.getDisplay(gDemo);
private static Command back =
new Command("Back", Command.BACK, 1);
private static Command go =
new Command("Go", Command.SCREEN, 1);
private static final int ORIGINAL_STATE = 1;
private static final int TRANSLATED_STATE = -1;
// The x coordinate of the initial drawing.
private int x = 20;
// The y coordinate of the initial drawing.
private int y = 20;
// The translation amount in the x direction.
private int deltaX = 30;
// The translation amount in the y direction.
private int deltaY = 30;
// State variable that tells the program if the drawing
// on-screen is in the original position or the
// translated position.
private int state = ORIGINAL_STATE;

/**
Constructor.
*/
public TranslationDemo()
{
super();
addCommand(back);
addCommand(go);
setCommandListener(this);
display.setCurrent(this);
}
protected void paintClipRect(Graphics g)
{
int clipX = g.getClipX();
int clipY = g.getClipY();
int clipH = g.getClipHeight();
int clipW = g.getClipWidth();
int color = g.getColor();
g.setColor(WHITE);
g.fillRect(clipX, clipY, clipW, clipH);
g.setColor(color);
}
public void paint(Graphics g)
{
int w = 50;
int h = 50;
paintClipRect(g);
g.fillRect(x, y, w, h);
}
// Toggle the state of the drawing. This method is
// called during processing of the "Go" command, which
// toggles the translation.
private void toggleState()
{
state = -state;
}
// Toggles the translation. Redraws the Canvas.
private void toggleTranslation()
{
if (state == ORIGINAL_STATE)
{
x = x + deltaX;
y = y + deltaY;
}
else
{
x = x - deltaX;
y = y - deltaY;
}
toggleState();
// Request the implementation to call the paint()
// method to repaint the canvas. This results in
// the generation of an internal paint event that
// is handled by the implementation.
repaint();
}
public void commandAction(Command c, Displayable d)
{
if (c == back)
{
GraphicsDemo.getInstance().display();
}
else if (c == go)
{
toggleTranslation();
}
}
}




Comments(1)


1. What about GraphicsDemo (class...?) mentioned in line 18
(private GraphicsDemo gDemo = GraphicsDemo.getInstance();) ?



By: Achi at 2008-01-08 04:21:57

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:


Enter Code:
The Captcha image

Latest Tutorials

[2010-07-30]Code sample to Send SMS from a J2ME application.
[2009-05-29]Adding your own Application icon for your J2ME application (jar file)
[2008-08-18]Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
[2008-08-01]Datagrams in J2ME (UDP Programming sample)
[2008-08-01]Client Server in J2ME (Socket Programming sample)
[2008-08-01]Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
[2008-08-01]Using HTTP vs UDP vs Socket in J2ME
[2008-08-01]RMSCookieConnector - Using Cookies in J2ME
[2008-07-29]POST UTF-8 encoded data to the server in J2ME
[2008-07-10]lists, forms, choices, gauges, text fields, text boxes in J2ME
[2008-07-10]Using List to create a Menu and Menu items in J2ME
[2008-07-10]Using alerts and tickers in J2ME
[2008-07-07]J2ME Canvas sample to show games programming in J2ME
[2008-07-07]Timer and TimerTask example in J2ME
[2008-06-27]List of GPRS Access points for all countries

More Latest News

Most Viewed Articles (in last 30 days)
GUI components and menu based J2ME Applications.
Client Server in J2ME (Socket Programming sample)
Getting Started with J2ME
Code sample to Send SMS from a J2ME application.
J2ME Canvas sample to show games programming in J2ME
RMS Basics in J2ME
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
TextBox sample program in J2ME
'double buffering' Sample program in J2ME
Using List to create a Menu and Menu items in J2ME
Timer and TimerTask example in J2ME
Adding your own Application icon for your J2ME application (jar file)
What is J2ME?
Sample J2ME code that shows various functionality of RMS.
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
Most Emailed Articles (in last 30 days)
What is J2ME?
Download a file over a network in J2ME midlet
How to load J2ME applications to the IDEN handsets
Getting Started with J2ME
Y.S. Sun Green Building Research Center
Sample J2ME code that shows various functionality of RMS.
Sample Java program shows how to Read a file over a network using J2ME midlet
Types of configurations in J2ME
'LINK.EXE' is not recognized as an internal or ext
RMS Basics in J2ME
GUI components and menu based J2ME Applications.
Your first J2ME program and a midlet lifecycle explained.
The MIDP Networking Model in J2ME
What is J2ME?
paint() sample program to draw a line in J2ME