translate() Sample program in J2ME
By Baski Viewed: 5698 times Emailed: 170 times Printed: 171 times
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 |
Latest Tutorials
| [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 |
| [2007-12-06] | What is J2ME? |
Most Viewed Articles (in last 30 days)
