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.
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|
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.