J2ME is a popular platform for developing games for wireless devices. With MIDP 2.0, a new package has been introduced that provides several gaming constructs that would only have been possible in MIDP 1.0 with a great deal of repetitive
code. In this part of this tutorial series, I will introduce you to this gaming package
and help you develop a simple game that uses all of the classes of this package as a learning tool. The package is called javax.microedition.lcdui.game,
and it builds upon the concepts that you learned in the previous installments in this series.
J2ME Gaming API: An Overview
There are only five classes in the javax.microedition.lcdui.game
package: GameCanvas, Layer, Sprite, TiledLayer,
and LayerManager. These five classes are enough to provide a platform
for the development of games with a wide range of capabilities.
The Layer class is the superclass of the Sprite and TiledLayer
classes. This class abstracts the behavior of a visual element in a game. This
element can be a sprite, which represents an independent graphic (or a collection
of graphics for animation) that can be moved around the game screen, or a tiled layer,
which represents a graphic that can be used to create vast game backgrounds
with only a handful of images. You use the
Layer classes for positioning and visibility. The subclasses override the paint(Graphics
g) method, which has the task of rendering the elements on the screen.
The LayerManager class provides a convenient mechanism to manage the
various visual elements of a game (sprites and tiled layers) by rendering the proper layer in the proper order.
The GameCanvas class is made useful by extending the functionality
of the Canvas class (from the previous article).
It provides an off-screen buffer, to which all rendering operations are done
before flushing them on the device screen. It also provides an easy-to-use mechanism
to query the current keys being pressed by the user.
The best way to introduce you to these classes is with the help of a working game example, which we will build from the ground up, explaining the various
facets of a game. This is helpful whether or not you have programmed a game before, if you are looking to learn
how to do it for the wireless devices using J2ME's gaming API. After a quick introduction to game building (for those who have never created a game), the remaining sections introduce each of the classes from the javax.microedition.lcdui.game package, with the help
of a concise but complete game.
A Very Short Primer on Game Building
A game or animation is built according to the principle of repetitively executing
a piece of code. This piece of code tracks the value of instance variables and
updates the game state accordingly. Based on the game state, the code then draws/paints/repaints
the game screen with the elements that make up the game. The values of the instance
variables may change because of either user interaction or internal game behavior.
The repetitive execution is effected by putting the repetitive code in an infinite
loop. Before entering the loop, an instance variable may be checked to see if
the game should still be running, and if not, the loop may be exited. The code
in the loop should allow the current thread of execution to sleep every few
milliseconds to control the rate at which the update to the instance variables
is done (in effect, how fast the game screen should be refreshed).
To put it in coding terms:
// main class
public MainClass {
private GameCanvas canvas = new MyGameCanvas();
public MainClass() {
// start a thread that will run infinitely
canvas.start();
}
// rest of the code
}
// the actual drawing class
public MyGameCanvas extends GameCanvas implements Runnable {
public MyGameCanvas() {
// instantiation code
}
public void start() {
// do initialization
// and then start a thread
Thread runner = new Thread(this);
runner.start();
}
private void run() {
// or while(keeprunning = true)
// where keeprunning is an instance variable
while(true) {
// checks if the game has reached
// some boundary states or special conditions
verifyGameState();
// gets input from user and
// updates instance variables
// that describe the games elements
checkUserInput();
// paints elements on screen to reflect
// the current game state using the current
// graphics object
updateGameScreen(getGraphics());
// control the rate at which screen updates are done
Thread.sleep(milliseconds);
}
}
}
We will use this structure to develop a game in the following sections.
J2ME Tutorial, Part 2: User Interfaces with MIDP 2.0
In part two of the J2ME tutorial you will create the user interface (UI) elements
of a MIDlet. Since the interaction with a user is a paramount concern in any MIDlet, due to the size of the screens, it is important for you to understand the basics of this side of MIDlets. Any interaction with a user is done via a UI element.
J2ME Tutorial, Part 1: Creating MIDlets
Java 2 Micro Edition (J2ME) combines a resource-constrained JVM and a set of Java APIs for developing applications for mobile devices. Here is a step-by-step guide to creating MIDlets,
testing and deploying these MIDlets, and a look at the lifecycle of a MIDlet.