Professional Documents
Culture Documents
Animation
• Display has a mechanism for executing your
code in the user-interface thread. It has a
method, callSerially(), that accepts a
Runnable. When the user-interface thread is
ready, meaning when it has finished servicing
all repaint requests, it will execute the run()
method of the Runnable from the user-interface
thread.
Shortly: Multithreading and
Animation
public class AnimationCanvas You’d kick off the animation
extends Canvas by calling start(), which in
implements Runnable { turn would simply call run().
public start() {
run(); Inside run(), you update your
} state and call repaint() to
public void paint(Graphics g) { request the painting of a new
// Paint a frame of the animation. frame.
}
public void run() {
// Update our state. Then you use callSerially() to
// Now request a paint of the new frame. request that you get called
repaint(); again when the painting is
Display.callSerially(this); done.
}
}
J2ME: The Game API
Overview
• The Game API builds on the Canvas and
Graphics classes
• The entire API is composed of five classes in
the javax.microedition.lcdui.game package:
• GameCanvas, that provides methods for
animation and key polling
• 4 classes, that deal with layers, which can
be used to compose scenes from several
different elements
Polling for Key States
• public int getKeyStates() - returns current state of the keys.
Example
Graphics g = getGraphics();
while(true) {
// Check for user input.
int ks = getKeyStates();
if ((ks & UP_PRESSED) != 0)
moveUp();
else if ((ks & DOWN_PRESSED) != 0)
moveDown();
// ...
// Update game state.
// Draw stuff using g.
flushGraphics();
}
Understanding Layers
• Layers are graphic elements that can be combined to
create a complete scene.
• Class javax.microedition.lcdui.game.Layer
• It has a location, a size, and can be visible or invisible:
• public final int getX()
• public final int getY()
• public final int getWidth()
• public final int getHeight()
• public void setPosition(int x, int y)
• Layer can be moved relatively to current position:
• public void move(int dx, int dy)
• The layer’s visibility is accessed using getVisible() and
setVisible()
Managing Layers
• Game API includes LayerManager, a class
that handles most of the details for you
• Layers have an index, which indicates their
position front to back. A position of 0 is on top,
closest to the user
• Layers may be added to the bottom of the list
using this method:
• public void append(Layer l)
• You can add a layer at a specific location using:
• public void insert(Layer l, int index)
• getSize(), getLayerAt(), remove()
View window
• LayerManager includes the concept of a view
window, which is the rectangular portion of the
scene that will be drawn
• The assumption is that the overall scene is
larger than the screen of the device, so only a
portion will be drawn at any time.
• You can set the view window:
public void setViewWindow(int x, int y, int width, int height)
• To draw the scene:
public void paint(Graphics g, int x, int y)
TiledLayer