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:
- Start running the wireless toolkit by choosing: Start Button -> Sun Java (TM) Wireless Toolkit for CLDC -> Wireless toolkit.
- You should see a window like this one:
- 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
. - An additional window will be opened with plenty of options for various settings. At this stage, just press OK button.
- 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.
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 {
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
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:
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.
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.