6/18/09

Writing a Hello World Midlet(Eclipse)

Creating a "Hello word!" midlet using Bizu-Me


Create a new project by going to File-->New-->Project. Then choose J2ME Midlet Suite in the list and click the next button. Then, insert a name for your project in the Project. We will put Bizu-Me-Demo for this project.

Choose a device in from the combo box. We will stick to using the DefaultColorPhone for this project.

Right click on the Bizu-Me project, go to New-->Source folder and create a source folder. Name it src.

You should now have a project with the following hierarchy:


Next we need to set up the classpath and dependencies. Right click on Bizu-Me-Demo and select properties. If you checked out the Bizu-Me project in your eclipse, go to the Project's tab and add Bizu-Me.

If you don't have Bizu-Me project in your eclipse, you can either follow the Bizu-Me setup or download a jar'd version from the website and add it as an external jar.

Once the classpath has been set, right click on the Bizu-Me-Demo project source folder and choose "create a new class". Fill in the class name as Main, the SuperClass field as org.openbizu.me.MIDlet and a package name i.e "org.openbizu.demo".

When this is done, you should end up with the following code in your Main.java class;

public class Main extends MIDlet {


protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}

protected void pauseApp() {
// TODO Auto-generated method stub
}

protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
}

You will find out that there will be a red cross at the top indicating that there is an error. Add the following lines to the Main.java class you just created to fix this.

public Main() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
super();
}

Since you are extending the org.openbizu.me.MIDlet class, you need to infer the org.openbizu.me.MIDlet super class constructor which throws three exceptions.

Next, we need to create a panel and add it to the container. There are two types of panels;

  • simple panel.

  • composite panel.

You can only do custom drawing operations on a simple panel whereas if you use a composite panel, you can both attach components, commands and custom drawing operations onto the panel.

Since we are only going to display a "Hello world !" string, we will use a simple panel. Next create a new class, name it HelloPanel. You should end up with the following hierarchy:

To draw anything onto the panel, implement the paint method by adding the following lines to the paint method.

public class HelloPanel extends SimplePanel{

public void hideNotify() {
// TODO Auto-generated method stub
}

public void paint(Graphics g) {
g.setColor(0);
g.drawString("Hello word !", getWidth()/2 , ( getWidth() - Font.getDefaultFont().getHeight() )/ 2,
Graphics.HCENTER | Graphics.TOP );

}

public void showNotify() {
// TODO Auto-generated method stub

}
}

We first set the font colour to black and draw the "Hello world!" String in the middle of the screen.

Lastly, we need to create the HelloPanel in the Main class and add it to the container. The main class should look like this:

public class Main extends MIDlet {

private HelloPanel helloPanel;

public Main() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
super();
helloPanel = new HelloPanel();
getContainer().put(0, helloPanel);

}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}

protected void pauseApp() {
// TODO Auto-generated method stub
}

protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
}

The getContainer() method creates a new container and the put method adds the HelloPanel to the container. To run the midlet, right click on the main method in the package explorer and choose run as emulated .


If you checked the Bizu-ME project's source code out and added it to this project's build path and you encounter a java.lang.NoClassDefFoundError similar to this one:

java.lang.NoClassDefFoundError: org/openbizu/demo/Main: org/openbizu/me/MIDlet
at com.sun.midp.midlet.MIDletState.createMIDlet(+29)
at com.sun.midp.midlet.Scheduler.schedule(+52)
at com.sun.midp.main.Main.runLocalClass(+28)
at com.sun.midp.main.Main.main(+116)

right click on the Bizu-Me-demo project you just created and make sure you export the Bizu-ME project in the build path configuration.

If you don't see any error then you should be able to see the emulator running. Congratulations, you just created your first midlet using Bizu-Me. Piece of cake right?

No comments: