Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Drawing a line on canvas in Java ME

From Wiki
Jump to: navigation, search

This code snippet demonstrates how to draw lines in a Java ME MIDlet.

Article Metadata
Code ExampleTested with
Devices(s): Nokia E70, Nokia 5800 XpressMusic, Nokia 6021, Nokia 6131, Nokia C3-01, Nokia E7-00, Nokia Asha 206
Created: dekudin (26 Feb 2009)
Last edited: hamishwillee (30 Jul 2013)



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:

  1. - contains the MIDlet class.
  2. - contains the DrawingLineCanvas class, with implemented method paint which is used to draw a line.

Source file:

    // Canvas for drawing line
private DrawingLineCanvas canvas;
public DrawingLineMidlet() {
* Sets up canvas for drawing line.

private void setupCanvas() {
canvas = new DrawingLineCanvas();
canvas.setTitle("Drawing the line");
* From MIDlet.
* Called when the MIDlet is started.

public void startApp() {
// The initial display is the main form

Source file:

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.

Supplementary material

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.

  • The patched, executable application that can be used to test the features described in this snippet is available for download at
  • You can view all the changes that are required to implement the above-mentioned features. The changes are provided in unified diff and colour-coded diff (HTML) formats in
  • For general information on applying the patch, see Using Diffs.
  • For unpatched stub applications, see Example app stubs with logging framework.

Version Hint

Windows Phone: [[Category:Windows Phone]]
[[Category:Windows Phone 7.5]]
[[Category:Windows Phone 8]]

Nokia Asha: [[Category:Nokia Asha]]
[[Category:Nokia Asha Platform 1.0]]

Series 40: [[Category:Series 40]]
[[Category:Series 40 1st Edition]] [[Category:Series 40 2nd Edition]]
[[Category:Series 40 3rd Edition (initial release)]] [[Category:Series 40 3rd Edition FP1]] [[Category:Series 40 3rd Edition FP2]]
[[Category:Series 40 5th Edition (initial release)]] [[Category:Series 40 5th Edition FP1]]
[[Category:Series 40 6th Edition (initial release)]] [[Category:Series 40 6th Edition FP1]] [[Category:Series 40 Developer Platform 1.0]] [[Category:Series 40 Developer Platform 1.1]] [[Category:Series 40 Developer Platform 2.0]]

Symbian: [[Category:Symbian]]
[[Category:S60 1st Edition]] [[Category:S60 2nd Edition (initial release)]] [[Category:S60 2nd Edition FP1]] [[Category:S60 2nd Edition FP2]] [[Category:S60 2nd Edition FP3]]
[[Category:S60 3rd Edition (initial release)]] [[Category:S60 3rd Edition FP1]] [[Category:S60 3rd Edition FP2]]
[[Category:S60 5th Edition]]
[[Category:Symbian^3]] [[Category:Symbian Anna]] [[Category:Nokia Belle]]

This page was last modified on 30 July 2013, at 07:48.
117 page views in the last 30 days.