RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Go Fishing with the New MIDP 2.0 Game APIs

Game developers have been using MIDP since its inception, but it was only with the release of MIDP 2.0 that this platform included tools specifically made for game development. Learn how these five new classes can help you create richer, more smoothly-animated graphics.

ames and mobile devices seem to be a natural fit. Since MIDP 1.0 first became available, the majority of applications written for the platform have been games. As MIDP grew in popularity, the need for APIs specific to gaming also increased. This article discusses some of the enhancements of MIDP 2.0 that assist developers in writing games.

It's All About Rendering
A large part of writing games has to do with rendering images to the screen. Near real-time access to video memory is critical to ensure smooth, even display of images. If you can't control when the screen refreshes with graphical updates, there is a good chance that movements and on-screen animation may be jerky, or the game event may make two or three passes through the primary game loop before a single screen refresh takes place, interfering with the user's experience.

MIDP 2.0 introduced a few new classes that can assist you tremendously in creating rich and smoothly-animated graphics. You can find them in the javax.microedition.lcdui.game package:

  • GameCanvas
  • Layer
  • LayerManager
  • Sprite
  • TiledLayer
This article discusses just what each of these classes does and how they can make your life easier.

GameCanvas extends the MIDP Canvas class and provides a few tools for managing the state of a game. An off-screen buffer is supported along with a getGraphics() method. The getGraphics() method allows you to access to the Graphics instance at any point and time to render images to the screen. This is different than the Canvas class, where all painting must be done within the call to paint(), as this is the only context in which the Graphics object is exposed. Constant access to the Graphics instance allows visuals to be updated directly rather than waiting until the paint() method is called during a screen refresh. This is a big advantage when you're writing games.

Any manipulation of the Graphics object in GameCanvas automatically writes to the off-screen buffer. Changes are flushed to the screen with a call to flushGraphics(), which transfers pixel data from the off-screen buffer into video memory. This transfer is done at a low level in the device code and is very fast. The following example shows a square being drawn using the off-screen buffer.

Graphics g = getGraphics();
g.drawRect(10, 10, 100, 100);
The Graphics object is the same as what is passed to the Canvas.paint() method, however, any updates to the video display are written to the off-screen buffer. flushGraphics() then causes the square to actually appear on the screen.

Monitoring Key States
Another important aspect of writing games is having immediate access to the current state of a given key, as opposed to merely being given a notification that a key press has taken place at some point in the past. Games states often rely on finite control over the state of a key, such as the up, down, left, right, and fire keys.

GameCanvas offers some help here as well. The getKeyStates() method allows you to query specific key states directly. Although the return value for this method is an int, the value can represent multiple key states. Specific key states are discovered using the bitwise & operator. The following example checks for several key states:

int keyState = getKeyStates();

if ((keyState & LEFT_PRESSED) != 0) {
  System.out.println("Left Key Pressed");
if ((keyState & RIGHT_PRESSED) != 0) {
  System.out.println("Right Key Pressed");
if ((keyState & UP_PRESSED) != 0) {
  System.out.println("Up Key Pressed");
if ((keyState & DOWN_PRESSED) != 0) {
  System.out.println("Down Key Pressed");
if ((keyState & FIRE_PRESSED) != 0) {
  System.out.println("Fire Key Pressed");

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date