Hello (Canvas version): Difference between revisions

From Mobile Application Design
Jump to navigationJump to search
No edit summary
Line 6: Line 6:
#Sanity check. I know how it is, you've been up for three days solidly working on your gestural controller / MySpace worm / life size matchstick version of Yoda. You've written this code which looks bug free, but nothing's working. Start at the beginning with the Hello World skeleton application to check your environment and slowly build up your application again.  
#Sanity check. I know how it is, you've been up for three days solidly working on your gestural controller / MySpace worm / life size matchstick version of Yoda. You've written this code which looks bug free, but nothing's working. Start at the beginning with the Hello World skeleton application to check your environment and slowly build up your application again.  


* This article assumes you're using Eclipse + EclipseME to run this example. Steps may be slightly different for other devices.  
* This article assumes you're using Eclipse   EclipseME to run this example. Steps may be slightly different for other devices.  


=== Creating a project ===
=== Creating a project ===

Revision as of 11:17, 4 July 2007

HelloMidletCanvas

To get you started and make sure that your environment is working nicely - I've created a fully featured MIDlet which has but one purpose in life - to say hello. The Hello world program has a long and distinguished pedigree in the world of programming and for us it will serve two main purposes:

  1. Verify configuration. Creating and running this simple program lets us know that we've installed the basic software and setup our environment correctly.
  2. Sanity check. I know how it is, you've been up for three days solidly working on your gestural controller / MySpace worm / life size matchstick version of Yoda. You've written this code which looks bug free, but nothing's working. Start at the beginning with the Hello World skeleton application to check your environment and slowly build up your application again.
  • This article assumes you're using Eclipse EclipseME to run this example. Steps may be slightly different for other devices.

Creating a project

  • Right click anywhere in the blank space of the Package Explorer tab in Eclipse. Select New -> Project from the context menu. Select J2ME->J2ME Midlet Suite as the type of project.
  • Name your project something suitable - like HelloMidletCanvas, then click Next.
  • Choose your Device from the list. Anything will do, but if you have something exotic and you're feeling experimental - give it a go. Click Next again and then Finish.

Creating the class

  • Once your project has been created, right-click on the src folder and choose New->Class.</a>
  • Name your class HelloMidletCanvas or whatever you called your project, ensuring that you spell it exactly as you did earlier (including case). Ignore Eclipse if it tries to warn you against using the default package.
  • Once you've done that, copy and paste the text below into your file, overwriting anything that was there before (if you changed names, Eclipse may complain and ask you to update the name of the class). Save the file.
/**
 * HelloMidletCanvas
 * A simple program which demonstrates displaying a string in a Canvas. 
 * 
 * copyleft Michael Sharon 2006
 */

import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class HelloMidletCanvas extends MIDlet implements CommandListener {

	//variable to hold a reference to the display
	private Display myDisplay;

	//our exit command
	private Command exit;

	//the canvas we will be painting to
	private Canvas aCanvas;

	public HelloMidletCanvas() {

		//get a reference to the current Display object
		myDisplay = Display.getDisplay(this);

		//create the exit command
		exit = new Command("Exit", Command.EXIT, 1);

		//create a new canvas
		aCanvas = new MyCanvas("Mmmm... canvases.");

		//add the exit command
		aCanvas.addCommand(exit);

		//set a listener for the command
		aCanvas.setCommandListener(this);
	}

	public void startApp() {
		myDisplay.setCurrent(aCanvas);
	}

	public void pauseApp() {
	}

	public void destroyApp(boolean unconditional) {
	}

	public void commandAction(Command c, Displayable s) {
		if (c == exit) {
			destroyApp(false);
			notifyDestroyed();
		}
	}

	//The MyCanvas class takes care of painting the screen
	private class MyCanvas extends Canvas {
		private String message;

		public MyCanvas(String msg) {
			message = msg;
		}

		public void paint(Graphics g) {
			int h = getHeight();
			int w = getWidth();

			g.setColor(200, 100, 20);
			g.fillRect(0, 0, w, h);
			g.setColor(255, 255, 255);
			g.drawString(message, h / 2, w / 2, g.TOP | g.HCENTER);
		}

	}
}

Setup the JAD file

  • Double-click the HelloMidletCanvas.jad file in your Package Explorer. It should open up the EclipseME JAD editor.
  • Click on Midlets, then Add. Type HelloMidletCanvas in the Name field, skip the Icon field and head straight to the Class field. A button should appear, allowing you to select your HelloMidletCanvas source file.

Create a launch configration

  • Right-click your project again, choose Run->Run As from the context menu. Double click Wireless Toolkit Emulator to create a new configuration and name it something like Vanilla MIDlet.

The selected project should be HelloMidletCanvas. Click on the Midlet radio button under the Executable option and then click Search. It should easily find your default HelloMidletCanvas

  • Click Apply, then Run.

Troubleshooting

  • Whoa! Something exploded! Not cool. If you see something like this - that's fine, it just means that we forgot to compile our source file into a JAR file.
  • Right-click your project folder again and choose J2ME -> Create Package

Success!

  • Click the big Green Run button again. This time it should show signs of life and boot up your first Java program in whichever emulator you selected.

4144789993164732714151