Friday, January 15, 2010

Your First MIDlet

As I've said in the Getting Started tutorial, we will be using NetBeans IDE in making our applications for Java enabled mobile phones. If you prefer using the Java ME Platform SDK you can still make use of this tutorial but will have to follow a different set of instructions later on. This is because the Java ME Platform SDK doesn't come with the Visual Mobile Designer. No worries, we'll just come back to that later.

In this tutorial we will be creating our first, traditional "Hello World" MIDlet. Why a "Hello World" MIDlet? Because it's very good practice when learning how to program for a platform is to first find out how to make something show up on the screen and the simplest thing to draw on the screen would be text. Although we won't just be drawing text...

"So what's a MIDlet?", you say. Bah! Details...details. Who cares, right? Seriously, in simple terms, a MIDlet is a Java program you can run on mobile devices such as Java enabled cellphones. A MIDlet is to your phone what an "exe" is to a PC (excuse the bad analogy heh).

If you haven't already, open the NetBeans IDE or Java Me Platform SDK. You should see something similar to this:

Netbeans IDE 6.8

Choose "File" from the main menu and click on "New Project" or press CTRL+SHIFT+N. Select "Java ME" from the list of Categories and "Mobile Application" from the list of Projects. Click on the "Next" button.

Choose Project

On the next screen, you get to name your project and choose it's location on your hard drive. As an example I typed "prjTestMidlet" in that box. Be sure to place a check on both "Set as Main Project" and "Create Hello MIDlet" options before you hit the "Next" button.

Name and Location

Leave the "Emulator Platform" to the default one. For "Device", choose "DefaultCldcPhone1". Choose "CLDC-1.0" from "Device Configuration" and "MIDP 2.0" for the "Device Profile". There's more about these settings later but for now hit the "Finish" button.

Default Platform Selection

This is where we will see one of the differences between the Java ME Platform SDK and NetBeans IDE. If all went well, Java ME Platform SDK users should be seeing the source code for the MIDlet that was automatically generated. NetBeans IDE users should be seeing the VMD:

NetBeans IDE 6.8 - Visual Mobile Designer

Java ME Platform SDK 3.0 - Code Editor

Java ME Platform SDK users :
  • Change the "Hello" text into "Name" and the "Hello World!" into your name.
Java ME Platform SDK 3.0 - Edited source code.

NetBeans IDE users :
  • In the VMD window, click on the "Screen" tab if you haven't already.
  • Double click on the text "Hello" and type in "Name : ".
  • Double click on the text "Hello World!" and type in your name.
NetBeans IDE 6.8 - Visually edited form.

By the way, NetBeans IDE users can view the source code of the application by expanding the items of the treeview in the Projects panel.

Click on the "Run Main Project" icon from the toolbar, it's the green "Play" icon or (arrow pointing to the right), or you can just press F6 from your keyboard.

Run Main Project Icon

After it has finished compiling and building the program the emulator window will pop-up. You will immediately see the MIDlet's output on the device emulator's screen. If you have more than one MIDlet in the project you would instead see a menu listing MIDlets on the screen same as on a real phone.

NetBeans IDE 6.8 : Emulator output.

Java ME Platform SDK : Emulator output.

Painless, wasn't it? Now, about those "Default Platform Selection" settings we chose earlier. What you choose will depend on the phone you want your program to run on. Most phones at the time of writing already supports MIDP 2.0 and this in turn allows us to use new libraries that were added to Java ME specifically for multimedia and game development. From here on, the rest of the tutorials you will find here will be focused on MIDP 2.0 phones. You can find more info about MIDP here : Mobile Information Device Profile(MIDP)

The reason that we chose CLDC 1.0 is for compatibility. If you're targeting a specific phone model it's best to view the device specification for that phone from the manufacturers website. One of the more noticeable enhancements included in CLDC 1.1 is that added support for real numbers or floats. Unless you really need to use floats in your code I suggest sticking to CLDC 1.0 for the time being. You can also find resources on the internet on how to deal with floats on the CLDC 1.0 platform. You can find more info about CLDC here : Connected Limited Device Configuration(CLDC)

We chose "DefaultCldcPhone1" for the device to emulate because it has a screen resolution and bit depth commonly seen on phones today. Here's a table showing the different resolutions and bit depth of the default device configurations you can choose from:

Device Resolution Bit Depth Colored Touch Screen
ClamshellCldcPhone1240x32016 bitX-
DefaultCldcJtwiPhone1180x20816 bitX-
DefaultCldcJtwiPhone2180x20816 bitX-
DefaultCldcMsaPhone1240x32016 bitX-
DefaultCldcMsaPhone2240x32016 bitX-
DefaultCldcPhone1240x32016 bitXX
DefaultCldcPhone2240x32016 bitXX
DefaultFxPhone1240x32016 bitXX
DefaultFxTouchPhone1240x32016 bitXX

The device configurations listed above doesn't just define the visual appearance of the emulator but also the optional libraries or packages which extends the emulator's capabilities.

The MIDlet code that was generated for us by the NetBeans IDE or the Java ME Platform SDK is actually a Form based application much like how one would make an application on a PC. You can use this method for creating simple games that don't rely much on fast or continuous screen updates like word games and puzzles. The next set of tutorials will cover the use of lower level drawing routines and a game loop which you can use to make games which require continuous animation or real-time responsiveness like shooters, platformers, driving games, role playing games, etc.

Got a question? I said something completely wrong? Please don't hesitate to leave a comment.


kostasup said...

Thanks for the very enlighting guidance! I'm a Java ME Platform SDK user. So far, so good.

Would you please provide a way on how do I play this application on my cell phone?

Do I have to convert this package of folders and files into one ".jar"? Do I have to compile the .java file first?

In the user guide of the platform says: " right-clic on the project -> choose properties -> build category -> choose creating JAR"

I do this, but it actually creates nothing :/

I would be grateful if you have any idea on this.

Thank you in advance.

Kostas, Greece

kostasup said...

update: I went by this silly query I had.

I compiled the main project and located the .jar file in my disc.
Then, I dropped it in my phone ( a S/E T700 ) via cable and it runs just fine.

Thanks again for your guidance.

Unknown said...

thanks for your usefull document.
my netbean 6.8 have not device emulator's screen after compile my midlet .
in tools ->add java platform I dont know which file should assing for add java platform.

devlin said...

Hello ali,

If you look at the NetBeans IDE Download Page, you should download from the column with Java Me ticked.

If you didn't, You can download and install Sun Java Wireless Toolkit. This should allow you to detect and add a platform from NetBeans IDE.

I think it's best to download NetBeans with JavaME already included.

Anonymous said...

Best Best 5 Merit Casino Sites in India
Discover the best 5 merit casino sites in worrione India ⏩ Find the BEST betting sites ⏩ The 메리트 카지노 top 메리트 카지노 고객센터 5 merit casino sites in India.