Your first J2ME Application tutorial:: HiTME!  

Posted by Tarek in , , , ,

I'm bringing to you my very own first tutorial on how to create your first Java Mobile application along with the source code of HitME(my first java application) as a demonstration.
At first sight you may think the tutorial is too long, but I promise if you follow it you'll find most of the operations are very easy, I'm only using a little detailed explanation that makes it longer. You'll also find an attached full source code of HitME..

screenscreen2 

Advice: Be little patient, and if there is some step that you don't get, skip it and copy it as it is and you'll get it later during the implementation. If not, I'll be happy to answer all your questions but don't give up, this could really be your first step to be a famous developer! Let's get started

I assume you have a minimal background in java. Knowledge of methods, classes and objects is necessary. I also recommend using NetBeans IDE with mobility pack as the developing software. You also need to download and install some special software called the phone SDK that allows you to have access to the specific J2ME libraries. I recommend Wireless Toolkit (WTK) from Sun.

Start building your application by creating the first class. Since we're using Hitme as a demo app, we'll call our first class Hitme.

The most important step is to import java microedtion classes and the Random class we'll be using:

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.util.Random;

All J2ME applications must inherit from the class MIDlet found in javax.microedition.midlet package. This is done by using the keyword extends MIDlet beside your class initialization:





public class Hitme extends MIDlet implements CommandListener 
{
public void startApp() {

}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}
}

implements CommandListener is also added because inorder to use commands in the application (such as Back, Exit, Ok..etc) a command Listener class must be implemented to listen to commands when executed and pass them to the command action method inwhich you'll specify and relate each action to its command.





public void commandAction(Command cmd, Displayable display)
{

}

As you see, commandAction takes 2 parameters; A command, and the current display so it can execute a command only at the desired screen.


Now to the next step, creating our displayable stuff. A Displayble is an object that has the capability of being placed on the display. You can display a Textbox, a form, a list, an alert and other stuff. A form can contain a mixture of those stuff. In this application we'll only be using Alert and Form. We'll use a Form to display our text, and an Alert to display our random number.


We'll now create our first Displayable screen that launches when the application starts:





public Displayable mainScreen()
{
}

After creating the mainScreen method, we need to place some Displayable objects inside. A form for example, to create a Form: Form formname=new Form("Title");


So our form becomes: Form ourForm=new Form("Hello");


Now we need to add some stuff to our Form. Some text for example.


ourForm.append("Hit Me to generate random numbers, and hit enough when you've had enough ; )");


To add a command, as you see we have initialized our commands; hitme and exit outside the method so they can be accessible from the commandAction method. Inside our method, we will give those commands their parameters. This would be on the following form:


command command name=new Command("Title", Command.Action, Priority);


So HitMe and Exit buttons become:


HitME=new Command("Hit Me!", Command.OK, 1);
Exit=new Command("Enough", Command.Exit, 1);


This isn't enough, we need to place those commands in our screen. This is done by:


ourForm.addCommand(HitME);
ourForm.addCommand(Exit);


Finally in this method, we order the CommandListener to listen to commands from our form by placing ourForm.setCommandListener(this); at the end and return our Displayable; return ourForm;





Command HitME;
Command Exit;
Alert preview;

public Displayable mainScreen()
{
HitME=new Command("Hit Me!", Command.OK, 1);
Exit=new Command("Enough", Command.EXIT, 1);

Form ourForm=new Form("Hello");
ourForm.append("Hit Me to generate random numbers, and hit enough when you've had enough ;)");

ourForm.addCommand(HitME);
ourForm.addCommand(Exit);

ourForm.setCommandListener(this);

return ourForm;
}

Now that we've created our first Displayable, we need to know how to bring it to screen. we need to use the setCurrent method under the class Display. For convenience, just add the following method to your class:





 public void display(Displayable display)
{
Display.getDisplay(this).setCurrent(display);
}

Everytime you need to bring a Displayable to show on screen, a result of a command for example, use display(XXX); replacing XXX with your displayable name. And to bring our Displayable to screen when the app launches, we need to place it inside the startApp() method, so it becomes:





public void startApp() 
{
display(mainScreen());
}

Now we'll create another method that produces a random number, puts it in an Alert and display it to the user.
Since this tutorial is mainly about creating Mobile java applications, and I'm assuming you've got some background in java and I won't explain operations related to generating random numbers.





 public Displayable HiTME()
{
Random generator = new Random();
generator.setSeed(System.currentTimeMillis());

double number = generator.nextFloat();
double times=number*100;
int X=(int)times;
String theNumber=Integer.toString(X);

preview=new Alert("There you go:", theNumber,null, AlertType.INFO);

return preview;
}

If you undertood the mainScreen method, you'll get that this method creates a Displayable object of type Alert. And alert is constructed in the following form:


Alert("Title", "String to display",Image ,AlertType). The alert displays the numbers generated from the math operations above. An alert can only display a String, thats why we converted the generated Integer to String before using it inside the alert.


Now the final step and our application gets ready is to assign commands. Eventhough commands action names tell what they should do, they don't necessarily do it. You have to specify their actions yourself using the commandAction method we've created in the beginning.


Modifying the cmmandAction method:





public void commandAction(Command cmd, Displayable display)
{

if(cmd==HitME)
{
display(HiTME());
}
else
notifyDestroyed(); //Terminates the application
}

That's it, you've created your first J2ME application! With little modifications to these codes, you can come up with very creative applications. Discussions and feedbacks are very welcomed.. Good luck


Download HiTME!


Download Full Source Code

This entry was posted on Saturday, March 22, 2008 at Saturday, March 22, 2008 and is filed under , , , , . You can follow any responses to this entry through the comments feed .

3 comments

Anonymous  

Thanx Allot TGhazali
This was really helpful for me

Malek

September 7, 2008 1:56 PM

hello buddy.. this was helpful can u tell me more..

October 5, 2008 2:03 PM
Anonymous  

great post mate!!

my first j2me app is here.

thx

October 23, 2008 3:24 PM

Post a Comment

Post a Comment