translate() Sample program in J2ME

By: Baski  

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




Archived Comments


Most Viewed Articles (in J2ME )

Latest Articles (in J2ME)

Comment on this tutorial