This article is a demonstration of developing a random number game using Java2 Microedition Platform (J2ME). This article will give a brief idea about how simple it is to develop useful applications using J2ME. It is not an in-depth discussion on J2ME. It is assumed that the reader has working knowledge of programming in Java language.
J2ME is a Java-based platform for developing applications capable of running on small devices, most commonly, mobile phones. Using J2ME, anyone can easily develop useful Java applications to be run on mobile devices. These applications are called MIDlets and are managed by a software called application-management software (AMS) built in a mobile device. A J2ME application is executed on an emulator. When a J2ME application is built it creates two files, one with extension JAR and another with extension JAD. The JAR file is an archive containing the MIDlet class and JAD file is a descriptor file which describes the JAR file. The application can be executed on a Java enabled mobile device by copying the two files to the device.
A MIDlet undergoes through the following states in its life cycle:
The initial state is the paused state. The AMS activates the midlet and calls its
startApp() method causing it to move to the started state. The
destroyApp() method is called to destroy the midlet. A boolean parameter is passed to the
destroyApp() method to indicate whether the destruction is unconditional or optional. Optional destruction can be cancelled by throwing a
Using the Code
There are two main packages,
javax.microedition.lcdui, which are required in every
MIDlet. In J2ME, the
abstract Display class represents the main display screen and the form class represents one of many forms which can be displayed on the screen. Processing commands can be implemented by implementing the
CommandListener interface. The
CommandListener interface declares the
commandAction() method which must be implemented by the
import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.util.*;
public class RandomMidlet extends MIDlet implements CommandListener
Form form; Display display; TextField txtNumber Command cmdCheck,cmdExit,cmdAbout; int x,ctr;
startApp() method is used to create the user interface for the
MIDlet and register the
nextInt() method from the
Random class returns a value between
0 and the parameter passed to it.
public void startApp()
x=new Random().nextInt(101); ctr=0; form=new Form("Number Guess Game"); display=Display.getDisplay(this); txtNumber=new TextField("Enter a number
(Between 0 and 100)", "", 10, TextField.NUMERIC);
cmdCheck=new Command("Check",Command.OK,1); cmdExit=new Command("Exit",Command.EXIT,3); cmdAbout=new Command("About",Command.HELP,4); form.append(txtNumber); form.addCommand(cmdCheck); form.addCommand(cmdExit); form.addCommand(cmdAbout); form.setCommandListener(this); display.setCurrent(form); }
Implementing the other life cycle methods.
public void pauseApp()
public void destroyApp(boolean unconditional)
commandAction() method. The
commandAction() method receives two parameters. The
Command parameter denotes the source command which initiated the process and the
Displayable parameter denotes the object on which the event occurred.
Alert object is used to display a message in a dialog box. The first parameter of the
Alert constructor is the
title. The second parameter is the message to be displayed. The third parameter represents the image to be displayed. The fourth parameter is the alert type.
public void commandAction(Command c,Displayable d)
int n=Integer.parseInt(txtNumber.getString()); ctr++; if(n<x)
alert=new Alert("Not quite","Try a larger value ",null,AlertType.WARNING);
alert=new Alert("Not quite","Try a smaller value ",null,AlertType.WARNING);
alert=new Alert("You got it","Congrats!!!
You have made it in "+ctr+" attempts.",null,AlertType.INFO);
x=new Random().nextInt(101); ctr=0; }
alert.setTimeout(Alert.FOREVER); display.setCurrent(alert); }
Alert alert=new Alert("About Random Game",
"Programmed by Azim",null,AlertType.INFO);
Points of Interest
I have used Java(TM) ME Platform SDK 3.0 to develop this application. Other environments like Netbeans and Sun Java (TM) Wireless Toolkit 2.5.2_01 for CLDC can also be used to develop J2ME applications. When the application is run, the output is displayed on the emulator. The application can be executed on a Java enabled mobile device by copying the generated JAD and JAR files to the device.
- 15th July, 2011: Initial version