Adding a Canvas

The previous tutorial dealt with starting a Java ME project in NetBeans and creating a MIDlet class. This time we will add a simple Canvas class to our project, which does some basic graphics commands.

Adding a Class in NetBeans
To start, right-click on your package in the Projects tree, select “New,” and then select “Java Class…” from the context menu. In the resulting dialog box, type in a class name of your choosing for the canvas class, and click Finish. You should end up with a file similar to the one below (as usual, the automatic comments have been removed for brevity).

MyCanvas.java

package com.dbarnes.demomidlet;public class MyCanvas {
 
public MyCanvas() {
}
 
}
 

First, lets import the classes in the javax.microedition.lcdui package, and modify our new class to be a subclass of Canvas. Note that in order to extend the Canvas class, we will also need to implement the abstract paint() method.


package com.dbarnes.demomidlet;
import javax.microedition.lcdui.*;
 
public class MyCanvas extends Canvas {
 
public MyCanvas() {
}
 
public void paint(Graphics g) {
}
 
}
 

We now have a valid subclass of Canvas that will compile. Now we will add some simple graphics commands to the paint() method.


public void paint(Graphics g) {
// Set the current color to black
g.setColor(0×000000);
 
// Clear background by filling the entire canvas
g.fillRect(0, 0, getWidth(), getHeight());
 
// Set the current color to red
g.setColor(0xFF0000);
 
// Get a Font object
Font font = Font.getFont(Font.FACE_SYSTEM,
Font.STYLE_BOLD, Font.SIZE_MEDIUM);
 
// Set the current font
g.setFont(font);
 
// Draw the string in the center of the canvas
g.drawString("This canvas is flippin' sweet!",
getWidth() / 2,
getWidth() / 2,
Graphics.BASELINE | Graphics.HCENTER);
 
}
 
Now our canvas, when painted, will display a centered red string on a black background. However, the MIDlet will not display the canvas until we add some code to startApp() telling it to do so. Go back to your subclass of MIDlet and add the following code.


public void startApp() {
// Get display
Display display = Display.getDisplay(this);
 
// Create an instance of our canvas
MyCanvas canvas = new MyCanvas();
 
// Set our canvas as current
display.setCurrent(canvas);
 
}
 
Run Mobile Project in NetBeans

We now have a MIDlet that will create a canvas and display it. We can test our MIDlet in the Java ME emulator. Right click on your project in the Projects tree, right-click, select “New,” and then select “Run Project” from the context menu.

Emulator in NetBeans

Now, click the SELECT button on the emulator, and you should see the screen displayed in the screenshot. Congratulations if you’ve made it this far. At this point, you should download the MIDP reference documentation if you haven’t already, and experiment with these and other graphics APIs.

The source code for the project described in this tutorial can be downloaded here: Source Code

 

Bookmark this article: del.icio.us Digg Furl Reddit blogmarks Google Spurl StumbleUpon Technorati Yahoo!

Leave a Reply

You must be logged in to post a comment.