Namespaces
Variants
Actions
Revision as of 07:43, 2 October 2012 by hamishwillee (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Drawing a line on canvas in Java ME

Jump to: navigation, search

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

SignpostIcon Palette 52.png
SignpostIcon Asha UI.png
Article Metadata

Code Example
Tested with
Devices(s): Nokia E70, Nokia 5800 XpressMusic, Nokia 6021, Nokia 6131, Nokia C3-01, Nokia E7-00, Nokia Asha 206

Compatibility
Platform(s): Series 40, Symbian

Article
Keywords: javax.microedition.lcdui.Canvas, javax.microedition.lcdui.Graphics, javax.microedition.lcdui.Canvas.paint, javax.microedition.lcdui.Graphics.drawLine
Created: dekudin (26 Feb 2009)
Last edited: hamishwillee (02 Oct 2012)

Contents

Overview

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. DrawingLineMidlet.java - contains the MIDlet class.
  2. DrawingLineCanvas.java - contains the DrawingLineCanvas class, with implemented method paint which is used to draw a line.

Source file: DrawingLineMidlet.java

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


Source file: DrawingLineCanvas.java

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


Postconditions

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 Media:DrawingLine.zip.
  • 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 Media:DrawingLine.diff.zip.
  • For general information on applying the patch, see Using Diffs.
  • For unpatched stub applications, see Example app stubs with logging framework.
222 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved