Java Reference
In-Depth Information
CHAPTER 13
■ ■ ■
Programming a Custom
User Interface
C hapters 5, 6, and 7 were devoted to MIDP's generalized user-interface APIs. Clever as these
APIs are, they are unsuitable for game development and other specialized user interfaces.
Games are programmed “closer to the metal” than other applications. MIDP offers a class,
javax.microedition.lcdui.Canvas , that provides low-level access to a device's screen and
input facilities. You can find out exactly which keys a user is pressing and draw whatever you
want on the screen.
The Game API, contained in the javax.microedition.lcdui.game package, includes a
refinement of Canvas and classes for layer-based graphics and sprite animations. The Game
API is fully discussed in Chapter 14. In this chapter, you'll learn the basics of Canvas and
drawing with the Graphics class.
The Canvas Class
Canvas is the heart of MIDP's custom user-interface API. To use it, you must create a subclass
of Canvas . This differs from the Screen subclasses, which are ready to use “out of the box.”
Aside from that, however, Canvas fits in very nicely with the other subclasses of Displayable .
A MIDlet can mix and match regular screens and Canvas es. In a game, for instance, a high-score
screen might be a Form , while the game itself would be played on a Canvas .
Canvas contains event-handling methods that are invoked by the MIDP implementation
whenever something important happens. When the user presses a key, or when the screen
needs to be painted, one of Canvas 's methods will be called. Most of these methods have empty
implementations in Canvas . To respond to an event, you need to override the appropriate
method and provide an implementation.
The one exception to this rule is the paint() method, which is declared abstract and thus
must be defined in subclasses.
Canvas Information
If you would like to draw your own user interface, you'll need some basic information about
the Canvas . You can find out the size of the Canvas by calling getWidth() and getHeight() . As
we'll discuss later, you can also find out the color capabilities of the device by calling methods
in Display .
231
Search WWH ::




Custom Search