Java Reference
In-Depth Information
There is also a full-screen mode. Some
Canvas
implementations won't occupy all the avail-
able screen space, reserving areas of the screen for information about the state of the device or
other purposes. If the device supports an alternate full screen mode for
Canvas
, you can use it
by calling
setFullScreenMode(true)
. Setting full screen mode on or off may result in calls to the
sizeChanged()
method
Canvas
inherits from
Displayable
.
Canvas
also features event handler methods that will be called by the MIDP implementa-
tion as your
Canvas
is displayed and hidden. Each time your
Canvas
is shown, the
showNotify()
method will be called. If another
Displayable
is shown, or the application manager decides to
run a different application,
hideNotify()
is called.
Painting and Repainting
The MIDP implementation calls a
Canvas
's
paint()
method when the contents of the
Canvas
need to be shown. This
paint()
method should look familiar to anyone who has ever imple-
mented a custom Swing or AWT component.
The MIDP implementation passes a
Graphics
object to your
paint()
method.
Graphics
has methods for drawing shapes, text, and images on a
Canvas
. A typical
Canvas
implementa-
tion, then, looks something like this:
import javax.microedition.lcdui.*;
public class JonathanCanvas
extends Canvas {
public void paint(Graphics g) {
// Draw stuff using g.
}
}
What if you want to tell the
Canvas
to draw itself? You can't call
paint()
directly, because
you don't have a suitable
Graphics
to pass to
paint()
. Instead, you need to tell the MIDP imple-
mentation that it's time to paint the
Canvas
. The way you do this is by calling
repaint()
. The
first version of this method simply tells
Canvas
to paint everything.
public void repaint()
public void repaint(int x, int y, int width, int height)
The second version is a way of saying, “I only want you to paint this rectangular portion of
the screen.” If the drawing you're doing is very complicated, you can save some time by only
painting the portion of the
Canvas
that has changed. This is implemented using a technique
called clipping. A later section discusses clipping in more detail.
How exactly does
repaint()
work? When you call
repaint()
,
paint()
won't be called right
away. The call to
repaint()
just signals to the MIDP implementation that you want the screen
to be painted. Some time later, the implementation
services
the repaint request, which results
in an actual call to the
paint()
method of the
Canvas
. The MIDP implementation may even
combine several repaint requests, particularly if their repaint regions overlap.
Search WWH ::
Custom Search