Java Reference
In-Depth Information
To run this example, you'll need a corresponding MIDlet to display KeyCanvas . At this
point, we think you should be able to do this by yourself.
Pointer Events
Some devices, particularly PDAs, may support a pointer. The popular Palm platform, for
example, is based around the use of a stylus and a touch-sensitive screen. You can find
out at runtime if your device supports pointer events by calling hasPointerEvents() and
hasPointerMotionEvents() . If the device supports pointer events, the following methods get
called when the pointer is pressed and released:
protected void pointerPressed(int x, int y)
protected void pointerReleased(int x, int y)
If the device supports pointer motion events, the following method will be called as the
user drags the stylus around the screen:
protected void pointerDragged(int x, int y);
Double Buffering
Double buffering is a well-known technique for reducing flicker in drawing and animations.
Imagine you are implementing an animation that clears and redraws the entire screen for each
frame of the animation. Without double buffering, the animation will flicker badly as the screen
is cleared and redrawn. With double buffering, the new frame is drawn into an off-screen image
(the buffer). When the off-screen drawing is complete, the image is drawn on the screen in one
smooth, quick move. You pay a price in the memory that's needed for the off-screen image, but
the improvement in the quality of the animation is dramatic.
The MIDP implementation may provide double buffering by default. You can find out
whether a Canvas is double buffered by calling the is DoubleBuffered() method.
If the implementation does not give you double buffering, you'll have to do it yourself.
Fortunately, it's not terribly difficult. The process looks like this:
Create an off-screen image by calling the static Image.createImage(int width, int height)
method.
1.
2.
Obtain a Graphics that draws into the image by calling getGraphics() on the Image .
Draw stuff into the off-screen image using the Graphics object.
3.
In the paint() method of the Canvas , use drawImage() to put the off-screen image on
the Canvas .
4.
Here's a Canvas subclass that creates a simple off-screen image and displays it:
Search WWH ::




Custom Search