Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Revision as of 17:07, 22 November 2007 by SergioEstevao (Talk | contribs)

Developing a 2D game in Java ME - Part 1

From Wiki
Jump to: navigation, search

So you want to develop for your mobile phone? Say hello to Java Micro Edition. JavaME combines a small JVM and a set of Java APIs for developing applications for mobile devices.

This article is the first in a series where we are going to approach all the basics for developing an application for the mobile phones using JavaME. The final goal of these tutorials is to develop a small Arkanoid clone ( and in the process learn all the base libraries, classes and methods available in JavaME:

This time, after a quick introduction to the development tools, I will provide a step-by-step guide to creating your first JavaME application, also known as MIDlets.

Development Tools

The development environment you use has a big influence on your productivity.

After trying out a lot of different IDEs, I narrowed my options to the following two:

Both IDEs are completely free and have a strong community backing them up. They allow you to do all the work you need to develop mobile applications.

Netbeans is very easy to install and configure and its interface is more intuitive than Eclipse, but it uses a lot of memory and when your project gets bigger it can be very slow.

Eclipse is a lot faster and has a smaller memory footprint, it has the same features that Netbeans but its interface is more awkward to use and you will have some more work to configure it right.

So, what's the best for you? If you already have experience with Eclipse it's smaller memory footprint and lightning speed are a big advantage, but if you are beginner with Java IDEs and mobile programming, Netbeans is a lot easier to start with.

Phones SDKs

Besides the Java IDE with mobility support you choose, you need to download and install a special software called the phone SDK that allows you to have access to the specific J2ME libraries.

Sun has an emulator called Wireless Toolkit (WTK) that provides all the standard libraries. I regularly use this one for my own development because it's very fast, has a lot of extra tools ( Network Monitor, Profiler, SMS Emulator etc...) and can be customized to look and act as several phones.

Besides the WTK it's also a good idea to have some SDKs from the phones manufacturers that you wish to support. These SDKs allows you to access the special libraries that they might use and often include software that allow you to deploy your applications to the phone.

All the big brands of cellular phones have their developers sites where you can download their phone emulators and custom SDKs, please check the Phones SDK links on the right side.

Your First Midlet

After you installed and configured your Java IDE and phone emulator, it's time to start coding!

All the J2ME applications must extend the abstract Midlet class found in the javax.microedition.midlet package, much like creating an applet by extending the java.applet.Applet class. The base entry point is the startApp() method.

So lets create our first Midlet named MyMidlet.

public class MyMidlet extends MIDlet{
// it's invoked when the application starts and each time is resumed
protected void startApp() throws MIDletStateChangeException {}
// it's invoked when the Midlet needs to be destroyed
protected void destroyApp(boolean uncondicional) throws MIDletStateChangeException {}
// it's invoked when the Midlet needs to be paused
protected void pauseApp() {}

If you create this class and run it in the emulator you will have a fantastic blank screen!

In order to show some content in your application you need to use the Display class, this class controls what appears on the screen of the Midlet. Each Midlet has one Display and can access it trough the Display static method getDisplay().

To present something on the screen you need to set a Displayable object using the setCurrent() method. One class that implements Displayable is the Alert class. This class shows a simple message on the screen.

With this information lets change our startApp() method to something more useful.

Alert alert;
//entry point for the application
protected void startApp() throws MIDletStateChangeException {
// creates alert
alert = new Alert("Hello World");
// shows alert in the screen.

If you run your Midlet now you will see a screen with the "Hello World" like the one below:


That's a little better but now you still don't have a way to exit your Midlet properly ( you can always press the red button!), in order to have a proper exit lets add some more lines of code to our startApp() method:

// create command
comExit = new Command("Exit", Command.EXIT, 1);
// add a command to exit the Midlet

This shows you the exit command on the screen but if you click on it, it doesn't do anything because you need to add a CommandListener to your Alert in order to react to Command Actions. So let's add some more code:

public class MyMidlet extends MIDlet implements CommandListener{
public void startApp(){
// adds a listener to the alert
public void commandAction(Command cmd, Displayable display) {
// check what command was selected
if (cmd == comExit) {

With this change we finally managed to have a complete functional Midlet. Yeah!!

In the next lesson we are going to get in to more details of the user interface elements like the Alert, Display and CommandListener classes. We are going to use those elements to define the menu interface for our Arkanoid clone, see you soon.


503 page views in the last 30 days.