Java Reference
In-Depth Information
CHAPTER 14
■ ■ ■
The Game API
M
IDP is an attractive platform for games, the leading edge of consumer J2ME software. In
the next chapter, you'll read about the exciting new 3D graphics capabilities available, and in
Chapter 16 you will read about MIDP's support for multimedia. This chapter describes the
Game API, which simplifies writing 2D games.
Overview
The Game API builds on the
Canvas
and
Graphics
classes you read about in Chapter 13. The
entire API is composed of five classes in the
javax.microedition.lcdui.game
package. One
class,
GameCanvas
, provides methods for animation and key polling. The other four classes deal
with layers, which can be used to compose scenes from several different elements.
GameCanvas
offers two main advantages over
Canvas
. First, your application has control
over exactly
when
the display is updated, instead of having to wait for the system software to
call
paint()
. Second, you can control what region of the screen is updated.
GameCanvas
gives
your application very specific control of display updates.
Driving Animation with GameCanvas
GameCanvas
extends
javax.microedition.lcdui.Canvas
with methods for animation and key
state polling.
GameCanvas
is used differently from
Canvas
:
•To use
Canvas
, you subclass it and define the
paint()
method. Inside
paint()
, you use
Graphics
to render graphics on the screen. When you change something and want to
update the screen, you call
repaint()
, and the system calls
paint()
again for you.
•To use
GameCanvas
, you subclass it. To draw on the screen, you use the
Graphics
returned
from
getGraphics()
. When you want updates to appear on the screen, call
flushGraphics()
,
which does not return until the screen is updated. For more specific updates, use the
method
flushGraphics(int x, int y, int width, int height)
, which only updates a
region of the screen.
public void flushGraphics(int x, int y, int width, int height)
GameCanvas
's model of use makes it easy to use inside a game loop like this:
255
Search WWH ::
Custom Search