paint() sample program to draw a line in J2ME

By: Reema sen  

The paint(Graphics g) method is the highlight of this example. Because Canvas defines this method to be abstract, subclasses must provide a concrete definition. Your program must perform all its drawing in its paint(Graphics g) method on the Graphics object passed to it. You invoke the Graphics class's dedicated drawing routines on this instance that is passed to your canvas.

To draw a line, you must specify the (x, y) coordinates of its start and end points. The (x, y) coordinates are defined relative to the point (0, 0), which, at the time the graphics context is created, represents the pixel at the top-left corner of the display. The x coordinate specifies the horizontal distance to the right from column 0 (the left edge of the display), and the y coordinate specifies the vertical distance down from row 0, which is the top of the display.

Lines are one pixel thick. To create thicker lines, you must draw adjacent lines. Additionally, the middle line appears dashed. You can set the stroke style for any drawing using the setStrokeStyle() method as demonstrated. The exact rendering of lines that use the Graphics.DOTTED stroke style is implementation-dependent.

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;
import javax.microedition.lcdui.Command;
Draws a series of lines to demonstrate the different
types and styles of lines that can be drawn with the
Graphics class.
@see javax.microedition.lcdui.Graphics
public class LineDemo extends Canvas
implements CommandListener
// A constant that represents the color white.
private static final int WHITE =
0xFF << 16 | 0xFF << 8 | 0xFF;
private Command back = new Command("Back",
private GraphicsDemo gDemo = GraphicsDemo.getInstance();
private Display display = Display.getDisplay(gDemo);
No-arg constructor.
public LineDemo()
Paints the clip rectangle white, effectively erasing
whatever was displayed on the Canvas previously.
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.fillRect(clipX, clipY, clipW, clipH);
Paints the look of this Canvas subclass.
public void paint(Graphics g)
int width = getWidth();
int height = getHeight();
g.drawLine(20, 10, width - 20, height - 34);
g.drawLine(20, 11, width - 20, height - 33);
g.drawLine(20, 12, width - 20, height - 32);
g.drawLine(20, 13, width - 20, height - 31);
g.drawLine(20, 14, width - 20, height - 30);
g.drawLine(20, 24, width - 20, height - 20);
g.drawLine(20, 25, width - 20, height - 19);
g.drawLine(20, 26, width - 20, height - 18);
g.drawLine(20, 36, width - 20, height - 8);
public void commandAction(Command c, Displayable d)
if (c == back)

Archived Comments

1. "paint() sample program to draw a line in J2ME"

Hello Dear,

View Tutorial          By: Carlos Alberto Urrea at 2015-02-06 14:00:14

Most Viewed Articles (in J2ME )

Latest Articles (in J2ME)

Comment on this tutorial