| ID | CS001313 | Creation date | March 30, 2009 |
| Platform | S60 3rd Edition, S60 5th Edition, Series 40 2nd Edition, Series 40 3rd Edition, FP1 | Tested on devices | Nokia E70, Nokia 5800 XpressMusic, Nokia 6021, Nokia 6131 |
| Category | Java ME | Subcategory | Graphics |
| Keywords (APIs, classes, methods, functions): javax.microedition.lcdui.Canvas, javax.microedition.lcdui.Graphics, javax.microedition.lcdui.Canvas.paint, javax.microedition.lcdui.Graphics.drawLine |
This code snippet demonstrates how to draw lines in a Java ME MIDlet.
The Canvas class is used for drawing lines and other simple graphics. Since Canvas is an abstract class, in order to draw lines, you must implement your own class based on Canvas and implement the paint method which is used for drawing on the canvas. After that, this class may be used as displayable - it can be assigned as the current displayable and it can contain commands.
This MIDlet consists of 2 source files:
// Canvas for drawing line
private DrawingLineCanvas canvas;
public DrawingLineMidlet() {
setupCanvas();
}
/**
* Sets up canvas for drawing line.
*/
private void setupCanvas() {
canvas = new DrawingLineCanvas();
canvas.setTitle("Drawing the line");
canvas.addCommand(EXIT_COMMAND);
canvas.setCommandListener(this);
}
/**
* From MIDlet.
* Called when the MIDlet is started.
*/
public void startApp() {
// The initial display is the main form
Display.getDisplay(this).setCurrent(canvas);
}
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
/**
* Canvas for drawing line.
*/
public class DrawingLineCanvas extends Canvas {
// Draw a diagonal black line
protected void paint(Graphics g) {
g.drawLine(10, 10, getWidth() - 10, getHeight() - 10);
}
}
A canvas with a diagonal black line on a white background is drawn on the display.
This code snippet is part of the stub concept, which means that it has been patched on top of a template application in order to be more useful for developers. The version of the Java ME stub application used as a template in this snippet is v1.1.
No related wiki articles found