Chapters 5 and 6 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, which 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.
Canvas is the heart of MIDP's game programming 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 Canvases. 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.
Team-Fly |