Click here to Skip to main content
Click here to Skip to main content

Introduction to JAVA ME Programming

, 10 Mar 2009
Rate this:
Please Sign up or sign in to vote.
A guide to write a basic JAVA Midlet

Introduction

JAVA ME (or in its full name JAVA Mobile Edition) is a small lightweight version of the JAVA language. Its running environments are focused on limited memory and/or processing power such as cellular phones, PDAs, TV set-top boxes, smart cards, etc.

Download SDK and Create Your First Project 

In this tutorial, we will learn how to write a simple "Hello world" application using the most elementary SDK available, the Sun Java ME Wireless toolkit which can be downloaded from here.
After installing the SDK, we can execute the following stages:

  1. Start running the wireless toolkit by choosing: Start Button -> Sun Java (TM) Wireless Toolkit for CLDC -> Wireless toolkit.  
  2. You should see a window like this one: 

    pic1.gif

  3. In order to create a new project, just press the "New Project" button. A new window will be opened and you will have to fill in a project name and a MIDlet (MIDlet class name will be explained later) class name. In our case, the project name will be Hello World and the MIDlet name will be HelloWorldMIDlet.
  4. An additional window will be opened with plenty of options for various settings. At this stage, just press OK button.

    pic2.gif

  5. At the end of the project creation, you will see in the main window information about where to place source files, resource files (such as pictures and sounds files), and application files. Java source files can be written in any text editor.

    pic3.gif

A Typical MIDlet Structure

A typical JAVA ME application consists of a Java source file and optionally you can add PNG files (for graphic files), MIDI and WAV files for audio files. The build and compile stage creates two files, JAR file which contains all the executable files and a JAD (JAVA descriptor) file. A JAD file contains information about the project such as vendor, CLDC and MIDP Configurations, and user defined definitions.
The core framework of the JAVA ME environment is the CLDC (Connected Limited Device Configuration).
CLDC contains the core classes of the JAVA ME environment, classes that handle IO functions, language functions and other basic utilities. The current version of CLDC is 1.1.

Alongside with the CLDC comes a profile. There are several profiles such as MIDP (Mobile Information Device Profile) for use with cellular phones and hand held devices. IMP (Information Module Profile) for use with other limited devices and DOJA for use with NTT DoCoMo i-mode mobile phones.

Every JAVA ME application must have at least one class which is extended from MIDlet class. This class actually manages the whole application. An application state defines the execution mode of the application. There are three runtime states of a JAVA ME application: "Active", "Paused" and "Destroyed" states.

  • An active state is the normal running mode of the application.
  • A paused state is invoked when an interruption occurs such as an incoming phone call.
  • A destroyed state is invoked when the MIDlet application ends its operation.

There are three abstract methods in a MIDlet class:

  • startApp(): is called when the MIDlet enters the active state.
  • pauseApp(): is called when the MIDlet enters the paused state.
  • destroyApp(boolean unconditional): is called when the MIDlet ends its execution.

These three methods are required for initializing and freeing resources and other user defined objects.
Our sample program is a simple MIDlet which displays "Hello World" on the cellular phone device.
As I mentioned before, every MIDlet has at least one class which is extended from MIDlet class. The name of the MIDlet has to be the name which was defined in the "New Project" stage. The skeleton class looks like this:

public class HelloWorldMIDlet extends MIDlet {

/**
* Occurs when the user terminates this program
* This function invokes when the MIDlet enters the "destroyed" state
*/
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		
}

/**
* Occurs when a program is interrupted by an external event
* such as incoming call 
* This function invokes when the MIDlet enters the "Paused" state
*/
protected void pauseApp() {
		
}

/**
* occurs every time when the program is starting.
* This function invokes when the MIDlet enters the "Active" state
*/
protected void startApp() throws MIDletStateChangeException {
}
}

There are several classes which are all extended from the Displayable class. These classes are classes that can be displayed on the screen. One such common class is the Canvas class. This class, which is very popular in game application, can draw, various objects on the screen, display pictures (in PNG format) and can handle animations too. Other Displayable classes are the List class which display menus and the Form class which displays plain text.
The Display class manages the display of the MIDlet. By this class, the users can decide which Displayable class should be displayed right now.
First of all, you have to define a Displayable class.

private Display display;

Then you have to initialize it:

/**
* constructor class
*/
public HelloWorldMIDlet() {
	display = Display.getDisplay(this);
}

As I mentioned before, the display class has to show a Displayable class. So now we have to create a class which is extended from the Displayable class.

/**
* This class represents an object to be displayed
* on the application. This class is a low level display object.
* Other high-level classes are: List and Form objects.
*/
private class MainScreen extends Canvas
{
	public void paint(Graphics g)
	{
		g.drawString("Hello World", 5, 5, Graphics.TOP | Graphics.LEFT);
}
}

In the constructor method, we initialize the MainScreen class:

mainScreen = new MainScreen();

And in the startup method, we set the mainScreen class to be displayed on the screen.

display.setCurrent(mainScreen);

Now, all that is left is to compile and run the midlet.

Conclusion

This has been only a basic MIDlet. Anyone who is interested in writing a full scale JAVA ME game can see my article here.
Another Java ME game with full source code can be found here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

krasnoff

Israel Israel
No Biography provided

Comments and Discussions

 
GeneralRe: MIDLET PinmemberMember 411560430-Jan-10 18:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 10 Mar 2009
Article Copyright 2009 by krasnoff
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid