Ads 468x60px

Saturday, July 31, 2010

Hello World Part 2

I think that everyone got the Hello World program running. Now, from this chapter I'm going to explain the coding part of the Hello World Program.

What is a MIDlet ?

MIDlet is the connection between the java application and the operating system in the MIDP(Mobile Information Device Profile) device. When you are creating a j2me application you are extending this MIDlet class ( javax.microedition.midlet.MIDlet ). Typical J2ME application must have extended at least one MIDlet in addition to all supporting classes which application uses.
MIDlet has three states in it's life cycle.

Let's take a look at MIDlet life cycle
MIDlet Life Cycle
    * Paused --> The MIDlet instance has been constructed and is inactive
    * Active  --> The MIDlet is active
    * Destroyed --> The MIDlet has been terminated and is ready for reclamation by the garbage collector.

 Basic MIDlet code

/*Code taken from */
import javax.microedition.midlet.*;

public class BasicMIDlet extends MIDlet {
    public BasicMIDlet(){
    // constructor - don't do much here

    protected void destroyApp( boolean unconditional )
          throws MIDletStateChangeException {
    // called when the system destroys the MIDlet

    protected void pauseApp(){
    // called when the system pauses the MIDlet

    protected void startApp()
          throws MIDletStateChangeException {
    // called when the system activates the MIDlet

In my example hello world I have implements the CommandLinstner interface in order to write Command Button actions. Alert class is used  to notify your user about anything important. Here it is used for just a simple message. and the alert.setTimeout(Alert.FOREVER) so up until you press OK the alert will be on the screen forever. That's it for this program. Next time we are going to talk about J2ME Low level User Interfaces.
So try out this example code of J2ME Low Level Interfaces Simple Drawing Example.
 * To change this template, choose Tools | Templates
 * and open the template in the editor.

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.*;

 * @author Aruna Sujith
public class SimpleDraw extends MIDlet {
    Display myDisplay;
    DrawShape myDrawShape;

    public SimpleDraw() {
        myDisplay = Display.getDisplay(this);
            myDrawShape = new DrawShape();

    public void startApp() {


    public void pauseApp() {

    public void destroyApp(boolean unconditional) {


class DrawShape extends Canvas {

    public void paint(Graphics g) {
        int w = getWidth();
        int h = getHeight();
        g.fillRect(0, 0, w, h);
        for (int x = 0; x < w; x += 10) {
            g.drawLine(0, w - x, x, 0);

        for (int x = 0; x < w; x += 20) {
            g.setColor(128, 0, 255 * x / w);
            g.fillRect(x, h - 30, 20, 20);



No comments:

Post a Comment