translate() Sample program in J2ME

By: Baski Emailed: 1728 times Printed: 2331 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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();
}
}
}


J2ME Home | All J2ME Tutorials | Latest J2ME Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(1)


1. View Comment

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


View Tutorial          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:



More Tutorials by Baski
Compiling multiple source files and specifying classpath using javac
The equals() Method example in Java
Default Values for Data Types in Java
JSP Example to connect to MS SQL database using Tomcat Connection Pool
<convertNumber> and <convertDateTime> in JSF
Enable/Disable Scripting Elements in JSP
Using calloc() Function in C
lseek() sample program in C
Arrays of Structures example program in C
The Basic Syntax Expression Language in JSP
Initialization of Pointer Arrays in C
Functions returning non-integer values in C
Increment and Decrement Operators in C
Getting Started with C
What is JSF (JavaServer Faces)?

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

More Latest News
Most Viewed Articles (in J2ME )
What is J2ME?
Code sample to Send SMS from a J2ME application.
List of GPRS Access points for all countries
Client Server in J2ME (Socket Programming sample)
Download a file over a network in J2ME midlet
Y.S. Sun Green Building Research Center
'LINK.EXE' is not recognized as an internal or ext
Sample Java program shows how to Read a file over a network using J2ME midlet
Sample J2ME code that shows various functionality of RMS.
java.lang.SecurityException: MIDP lifecycle does not support system exit.
Gauge sample program in J2ME
Sample program to draw a arc in J2ME
translate() Sample program in J2ME
'double buffering' Sample program in J2ME
RMS Basics in J2ME
Most Emailed Articles (in J2ME)
Your first J2ME program and a midlet lifecycle explained.
Sample program to draw a arc in J2ME
Types of Profiles in J2ME
Using HttpConnection in J2ME (Retrieve web content from a website to a phone)
Play a multimedia file in J2ME Program (Audio/Video) using MMAPI
What is J2ME?
Download a file over a network in J2ME midlet
How to load J2ME applications to the IDEN handsets
Y.S. Sun Green Building Research Center
'LINK.EXE' is not recognized as an internal or ext
Sample Java program shows how to Read a file over a network using J2ME midlet
Sample J2ME code that shows various functionality of RMS.
A sample J2ME midlet that shows an example of using a Timer and a TimerTask.
GUI components and menu based J2ME Applications.
java.lang.SecurityException: MIDP lifecycle does not support system exit.