Painting and the Graphics Class
When the platform determines that the
content of a Canvas needs to be drawn onto the
screen, it calls the paint( )
method,
which the MIDlet developer is required to implement:
protected void paint(Graphics g)
This method is called at the following times:
When the
Canvasbecomes visible as a result of theDisplaysetCurrent( )method being invokedWhen some or all of the
Canvasreappears after being partly or wholly obscured by anAlertor a system screen, such as a menu ofCommands opened from a soft buttonAs a result of application code requesting that the screen be repainted following a change in the data that it is rendering
The Graphics object passed to the paint( ) method provides methods that allow graphics operations,
such as line and text rendering and color filling, to be performed on
its target. The target is either the screen itself or, in the case of
a platform that supports double buffering, an off-screen image that
will be copied to the screen when the paint( )
method returns. Implementing this method is the only way to get a
Graphics object that can access the screen; unlike
the J2SE Component class,
Canvas does not have a getGraphics( ) method that can be used to get access on demand to the
screen space that it occupies. Therefore, all screen updates must be
performed in the paint( ) method. The MIDP
specification prohibits holding a reference to the
Graphics object passed to paint( ) for use elsewhere.[18]
When the visibility of ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access