Java Reference
In-Depth Information
public void stop() {
mTrucking = false;
}
public void render(Graphics g) {
int width = getWidth();
int height = getHeight();
// Clear the Canvas.
g.setGrayScale(255);
g.fillRect(0, 0, width - 1, height - 1);
int x = mBorder;
int y = mBorder;
int w = width - mBorder * 2;
int h = height - mBorder * 2;
for (int i = 0; i < 8; i++) {
g.setGrayScale((8 - i) * 32 - 16);
g.fillArc(x, y, w, h, mTheta + i * 10, 10);
g.fillArc(x, y, w, h, (mTheta + 180) % 360 + i * 10, 10);
}
}
public void run() {
Graphics g = getGraphics();
while (mTrucking) {
mTheta = (mTheta + 1) % 360;
render(g);
flushGraphics();
try { Thread.sleep(mDelay); }
catch (InterruptedException ie) {}
}
}
}
We'll assume you can write your own MIDlet to display SweepGameCanvas . If you've down-
loaded the examples, SweepGame is a MIDlet that displays SweepGameCanvas .
Polling for Key States
GameCanvas offers an alternative method for responding to key presses, which are expected to
be the way the user controls the game. Instead of passively waiting for the key event callbacks
defined in Canvas , GameCanvas offers a method that returns the current state of the keys:
public int getKeyStates()
Search WWH ::




Custom Search