Java Reference
In-Depth Information
import javax.microedition.lcdui.*;
public class OffscreenCanvas
extends Canvas {
private Image mImage;
public void paint(Graphics g) {
if (mImage == null)
initialize();
g.drawImage(mImage, 0, 0, Graphics.TOP | Graphics.LEFT);
}
private void initialize() {
int w = getWidth();
int h = getHeight();
mImage = Image.createImage(w, h);
Graphics g = mImage.getGraphics();
g.drawRect(0, 0, w - 1, h - 1);
g.drawLine(0, 0, w - 1, h - 1);
g.drawLine(w - 1, 0, 0, h - 1);
}
}
Multithreading and Animation
As with any graphic-interface toolkit, threading with the MIDP user-interface classes is a little
tricky. The user-interface implementation has its own thread that handles both user-interface
methods and screen painting. For example, when the user presses a key on their device, the
implementation calls keyPressed() in your Canvas subclass. The thread that calls this method
belongs to the MIDP implementation. As such, it should be handled with some care. In MIDP
implementations, the same thread that calls event methods also calls paint() .
Note All event-driven user-interface toolkits have this idea of a system-owned user-interface thread.
In AWT and Swing, it's called the event dispatch thread . The same rule applies: if you're running inside
a thread that doesn't belong to you, don't take all day about it.
Methods that are called by a thread that doesn't belong to you are callbacks. The rule of
thumb for callbacks is that you shouldn't do anything that takes a long time. Since the thread
doesn't belong to you, you shouldn't hold it up a long time performing your work. Because this
 
Search WWH ::




Custom Search