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
1. What about GraphicsDemo (class...?) mentioned in line 18
(private GraphicsDemo gDemo = Graphi
View Tutorial By: Achi at 2008-01-08 04:21:57
Comment on this tutorial
- Data Science
- Android
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
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
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
Client Server in J2ME (Socket Programming sample)
Datagrams in J2ME (UDP Programming sample)
POST UTF-8 encoded data to the server in J2ME
Using alerts and tickers in J2ME
Using List to create a Menu and Menu items in J2ME
lists, forms, choices, gauges, text fields, text boxes in J2ME
Timer and TimerTask example in J2ME