One of the main usages of J2ME is game programming. In this
article, I'll explain this by way of a standalone game that I
developed. This application is a simple basketball game, in which
the player plays against a computer opponent. Each game lasts
exactly two minutes. The aim of the game is to shoot as many baskets
as you can and to prevent the computer opponent from doing the
same.
Main Functions
A typical game in J2ME environment might consist of the
following:
A welcome screen that redirects automatically to the main
menu.
The main menu.
The main screen. This is where the game is actually
played.
Level menu (set the game difficulty, etc.).
Instruction screen.
About screen.
The main screen and the welcome screen are executed by classes
that are extends from the Canvas. This class is defined as being in the
low-level API, meaning that it allows full control of the
display at the pixel level. All the other screens are executed by
high-level API classes that use J2ME standard controls.
When we program apps for J2ME, we must also consider how to
handle unique system events such as call interrupts. When we deal
with this event, we might want to freeze the current game state or
even be able to save this game state after we exit the
application.
In order to get started, we'll have to download the J2ME
wireless toolkit. This is the most elementary SDK for this
environment. The main objective of this toolkit is to compile the
Java source files and to produce two deployment files: a JAR file
that encapsulate all of the class files and a JAD (Java Application
Descriptor) file. The JAD file provides data about the application
such as vendor, JAR size, version number, etc. This SDK can be
integrated with more advanced IDEs such as Eclipse or NetBeans. You can learn more about
getting started with the wireless toolkit in the article "
Wireless Development Tutorial Part I."
Game Structure
Figure 1 shows the class structure of the game.
Figure 1. General structure of the game
The game starts with an opening screen, and after five seconds
redirects to a menu screen. From the menu screen, the player
has the option to start the game, set the level, display an "about"
screen, or display the instructions. Each screen is managed by its
own class. For example, the welcome screen is managed by the
Splash class, the main game screen is managed by
MainScreen, and so forth.
The Midlet Class
A very important class that is not displayed in the diagram
above is the TestMidletMIDlet class. This class
extends from the Midlet class, and its job is to
initialize all of the other classes and controls. One major method of
the Midlet class is the startApp()
method. Normally, when a midlet is called, it is
initially in a paused state. If everything runs normally and
no exception occurs, then the midlet enters the active
state. This is done by calling the startApp() method.
Here is an example:
public void startApp() {
// some code here...
display.setCurrent(splash);
}
For this stage, the most important call is
display.setCurrent(splash). This method call sets the
midlet's Displayable; in other words, it determines which of
our screens is to be displayed on the device. In this example it
will be the Splash screen, as seen in Figure 2.
Figure 2. Splash screen
Splash Screen
The splash screen is the first screen that appears when the
application starts up. It is extends the Canvas class. The Canvas class, as the
so-called low-level API, allows full control of the display at the
pixel level. In order to paint something, we have to override the
paint() method. For example:
public void paint(Graphics g) {
// sets background color
g.setColor(255, 255, 255);
// fill the screen with the color
//defined previously
g.fillRect(0, 0, this.getWidth(),
this.getHeight());
// draw some image
g.drawImage(screenshot,
(this.getWidth() - 128) / 2,
(this.getHeight() - 128) / 2,
Graphics.TOP|Graphics.LEFT);
}
After a period of five seconds, or by pressing any key, our game
changes the display to the main menu screen. In order to handle key
presses we have to implement the CommandListener interface. All key press
events will be handled by the function
commandAction().
public void commandAction(Command command,
Displayable displayable) {
// stops timer operation
timer.cancel();
/*
switch the display to menu. parent
is the Midlet object which actually does the
job.
*/
parent.setCurrent("MainMenu2");
}
Timer and TimerTask are two classes that
allow us to execute a function after a period of predefined time.
We use these classes to automatically switch the display to the
next screen.
public void startTimer() {
// TimerTask defines the task or subroutine
// to be executed.
TimerTask gotoMenu = new TimerTask() {
public void run() {
timer.cancel();
parent.setCurrent("MainMenu2");
}
};
// Timer class creates and manages threads on
// which the tasks are executed
timer = new Timer();
timer.schedule(gotoMenu, 5000);
}
The MainMenu Class
The MainMenu class, shown in Figure 3, shows the
main menu of the game.
Figure 3. Main menu screen
The main menu has five elements:
Continue: Continues a game that has been previously
stopped.
Start Game: Starts a new game.
Choose Level: Choose the speed of the computer player.
Instructions: Shows an instructions screen.
About: Shows an about screen.
In J2ME, the class
List is responsible for displaying lists or menus, which
is why MainMenu subclasses it. As in the
Splash class, we have to implement the CommandListener interface in order to handle key
presses.
The MainScreen Class
MainScreen, shown in Figure 4, is the class that displays
the actual basketball game being played. The
MainScreen class is extended from the
Canvas class.