m3g12
Class RobotSample

java.lang.Object
  extended by javax.microedition.lcdui.Displayable
      extended by javax.microedition.lcdui.Canvas
          extended by javax.microedition.lcdui.game.GameCanvas
              extended by m3g12.RobotSample
All Implemented Interfaces:
java.lang.Runnable, Sample

public class RobotSample
extends javax.microedition.lcdui.game.GameCanvas
implements Sample, java.lang.Runnable

Displays a walking robot that can be controlled with the game keys.

Author:
Claus Hoefele

Field Summary
private  javax.microedition.m3g.AnimationController controller
          The robot's animation controller.
private static int DATA_DISTANCE
          State data index for walking distance.
private static int DATA_DURATION
          State data index for animation duration.
private static int DATA_FALSE_STATE
          State data index for next state if key was not pressed.
private static int DATA_KEY
          State data index for key to move to the next state.
private static int DATA_SPEED
          State data index for animation speed.
private static int DATA_START
          State data index for animation start time.
private static int DATA_TRUE_STATE
          State data index for next state if key was pressed.
private  float direction
          Current walking direction.
private  javax.microedition.lcdui.Graphics graphics
          2D graphics singleton used for rendering.
private  javax.microedition.m3g.Graphics3D graphics3d
          3D graphics singleton used for rendering.
private  boolean isRunning
          Flag for stopping the animation thread.
private static java.lang.String M3G_FILE
          File that stores the scene graph.
private  javax.microedition.m3g.SkinnedMesh mesh
          Robot.
private  javax.microedition.m3g.Group position
          The robot's position.
private  int state
          Current state.
private static int[][] STATE_DATA
          State data.
private static int STATE_IDLE
          State index for idle state.
private static int STATE_INVALID
          State index for invalid state.
private static int STATE_STARTING
          State index for starting state.
private static int STATE_STOPPING
          State index for stopping state.
private static int STATE_WALKING
          State index for walking state.
private  int stateTime
          Time since last state update.
private  javax.microedition.m3g.Transform transform
          Helper object for transformation.
private static int USER_ID_MESH
          User ID to find the mesh inside the scene graph.
private  javax.microedition.m3g.World world
          Object that represents the 3D world.
 
Fields inherited from class javax.microedition.lcdui.game.GameCanvas
DOWN_PRESSED, FIRE_PRESSED, GAME_A_PRESSED, GAME_B_PRESSED, GAME_C_PRESSED, GAME_D_PRESSED, LEFT_PRESSED, RIGHT_PRESSED, UP_PRESSED
 
Fields inherited from class javax.microedition.lcdui.Canvas
DOWN, FIRE, GAME_A, GAME_B, GAME_C, GAME_D, KEY_NUM0, KEY_NUM1, KEY_NUM2, KEY_NUM3, KEY_NUM4, KEY_NUM5, KEY_NUM6, KEY_NUM7, KEY_NUM8, KEY_NUM9, KEY_POUND, KEY_STAR, LEFT, RIGHT, UP
 
Constructor Summary
RobotSample()
          Constructor.
 
Method Summary
private  void changeState(int state, int worldTime, int error)
          Changes state and updates the animation position.
 void destroy()
          Destroys the sample.
private  void drawMenu(javax.microedition.lcdui.Graphics graphics)
          Draws a menu for the current key assignments.
private  int getData(int index)
          Returns data for the current state.
 void init()
          Initializes the sample.
private  void process(int worldTime, int stateDelta)
          Checks for key presses and advances the state machine.
protected  void render(javax.microedition.lcdui.Graphics graphics)
          Renders the sample.
 void reset()
          Initializes the sample.
 void run()
          Animation loop.
 
Methods inherited from class javax.microedition.lcdui.game.GameCanvas
flushGraphics, flushGraphics, getGraphics, getKeyStates, paint
 
Methods inherited from class javax.microedition.lcdui.Canvas
getGameAction, getKeyCode, getKeyName, hasPointerEvents, hasPointerMotionEvents, hasRepeatEvents, hideNotify, isDoubleBuffered, keyPressed, keyReleased, keyRepeated, pointerDragged, pointerPressed, pointerReleased, repaint, repaint, serviceRepaints, setCommandListener, setFullScreenMode, showNotify, sizeChanged
 
Methods inherited from class javax.microedition.lcdui.Displayable
addCommand, getHeight, getTicker, getTitle, getWidth, isShown, removeCommand, setTicker, setTitle
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

M3G_FILE

private static final java.lang.String M3G_FILE
File that stores the scene graph.

See Also:
Constant Field Values

USER_ID_MESH

private static final int USER_ID_MESH
User ID to find the mesh inside the scene graph.

See Also:
Constant Field Values

STATE_INVALID

private static final int STATE_INVALID
State index for invalid state.

See Also:
Constant Field Values

STATE_IDLE

private static final int STATE_IDLE
State index for idle state.

See Also:
Constant Field Values

STATE_STARTING

private static final int STATE_STARTING
State index for starting state.

See Also:
Constant Field Values

STATE_WALKING

private static final int STATE_WALKING
State index for walking state.

See Also:
Constant Field Values

STATE_STOPPING

private static final int STATE_STOPPING
State index for stopping state.

See Also:
Constant Field Values

STATE_DATA

private static final int[][] STATE_DATA
State data. Contains start time of animation, duration, speed, key assignment, true state, false state, and walking distance.


DATA_START

private static final int DATA_START
State data index for animation start time.

See Also:
Constant Field Values

DATA_DURATION

private static final int DATA_DURATION
State data index for animation duration.

See Also:
Constant Field Values

DATA_SPEED

private static final int DATA_SPEED
State data index for animation speed.

See Also:
Constant Field Values

DATA_KEY

private static final int DATA_KEY
State data index for key to move to the next state.

See Also:
Constant Field Values

DATA_TRUE_STATE

private static final int DATA_TRUE_STATE
State data index for next state if key was pressed.

See Also:
Constant Field Values

DATA_FALSE_STATE

private static final int DATA_FALSE_STATE
State data index for next state if key was not pressed.

See Also:
Constant Field Values

DATA_DISTANCE

private static final int DATA_DISTANCE
State data index for walking distance.

See Also:
Constant Field Values

stateTime

private int stateTime
Time since last state update.


state

private int state
Current state.


world

private javax.microedition.m3g.World world
Object that represents the 3D world.


mesh

private javax.microedition.m3g.SkinnedMesh mesh
Robot.


controller

private javax.microedition.m3g.AnimationController controller
The robot's animation controller.


position

private javax.microedition.m3g.Group position
The robot's position.


direction

private float direction
Current walking direction.


transform

private javax.microedition.m3g.Transform transform
Helper object for transformation.


isRunning

private boolean isRunning
Flag for stopping the animation thread.


graphics3d

private javax.microedition.m3g.Graphics3D graphics3d
3D graphics singleton used for rendering.


graphics

private javax.microedition.lcdui.Graphics graphics
2D graphics singleton used for rendering.

Constructor Detail

RobotSample

public RobotSample()
Constructor.

Method Detail

init

public void init()
Initializes the sample.

Specified by:
init in interface Sample

reset

public void reset()
Initializes the sample.


destroy

public void destroy()
Destroys the sample.

Specified by:
destroy in interface Sample

run

public void run()
Animation loop.

Specified by:
run in interface java.lang.Runnable

getData

private int getData(int index)
Returns data for the current state.

Parameters:
index - type of state data.
Returns:
data.

changeState

private void changeState(int state,
                         int worldTime,
                         int error)
Changes state and updates the animation position.

Parameters:
state - new state.
worldTime - current world time.
error - delta between the animation start and current world time.

process

private void process(int worldTime,
                     int stateDelta)
Checks for key presses and advances the state machine.

Parameters:
worldTime - current world time.
stateDelta - time since last state change.

render

protected void render(javax.microedition.lcdui.Graphics graphics)
Renders the sample.

Parameters:
graphics - graphics context for drawing.

drawMenu

private void drawMenu(javax.microedition.lcdui.Graphics graphics)
Draws a menu for the current key assignments.

Parameters:
graphics - graphics context for drawing.