Canvas is a Displayable that serves as the most fundamental class of the low-level user interface API. A Canvas can be thought of as a blank sheet of paper that covers the accessible area of a device’s screen. In order to use the Canvas class, a MIDlet must subclass it to provide drawing and event handling logic. When such a subclass is installed as a MIDlet’s current screen (using the Display.setCurrent() method) and the MIDlet is in the foreground, the paint() method will be called as necessary to cause the content of the Canvas to be drawn onto the screen. Similarly, events from the keyboard or a pointing device, if there is one, will be notified to the Canvas and can be handled appropriately by the MIDlet.

The abstract paint() method must be implemented to draw the content of the canvas onto the screen, using the Graphics object supplied as its argument. The clipping rectangle in this Graphics object may only cover a subset of the entire Canvas if only part of the screen needs to be repainted. In general, the paint() method should query the clipping rectangle and only perform graphics operations that would affect this part of the screen, where this can be economically determined.

A device may or may not double-buffer its screen to provide smoother updates and eliminate flashing caused by partial updates that are visible to the user. The process of double-buffering is transparent to the paint() method, but a MIDlet can determine whether double-buffering ...

Get J2ME in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.