import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.m3g.Camera;
import javax.microedition.m3g.Graphics3D;
import javax.microedition.m3g.Loader;
import javax.microedition.m3g.World;
import javax.microedition.midlet.MIDlet;
public class RMMIDlet extends MIDlet implements CommandListener {
private Display myDisplay = null;
private RetainedCanvas myCanvas = null;
private Command exitCommand = new Command("Exit", Command.ITEM, 1);
public RMMIDlet() {
super();
myDisplay = Display.getDisplay(this);
myCanvas = new RetainedCanvas();
myCanvas.setCommandListener(this);
myCanvas.addCommand(exitCommand);
}
public void startApp() {
myCanvas.init();
myDisplay.setCurrent(myCanvas);
myCanvas.start();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
myCanvas.stop();
}
public void commandAction(Command cmd, Displayable disp) {
if (cmd == exitCommand) {
try {
destroyApp(false);
notifyDestroyed();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
class RetainedCanvas extends GameCanvas implements Runnable {
private boolean mRunning = false;
private Thread mPaintThrd = null;
private Graphics3D mGraphics3D = Graphics3D.getInstance();
private World mWorld = null;
private Camera mCam = null;
private long mWorldStartTime = 0;
public RetainedCanvas() {
super(true);
}
public void init() {
try {
mWorld = (World) Loader.load("/p.m3g")[0];
mCam = mWorld.getActiveCamera();
mCam.translate(0, 0, -1.5f);
mCam.setOrientation(180, 0, 1, 0);
} catch (Exception e) {
e.printStackTrace();
}
mWorldStartTime = System.currentTimeMillis();
}
public void start() {
mRunning = true;
mPaintThrd = new Thread(this);
mPaintThrd.start();
}
public void stop() {
mRunning = false;
try {
mPaintThrd.join();
} catch (InterruptedException ex) {
}
}
public void run() {
Graphics g = getGraphics();
long startTime;
while (mRunning) {
cameraForward();
startTime = System.currentTimeMillis() - mWorldStartTime;
mWorld.animate((int) startTime);
mGraphics3D.bindTarget(g);
mGraphics3D.render(mWorld);
mGraphics3D.releaseTarget();
flushGraphics();
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
}
}
}
private void cameraForward() {
mCam.translate(0f, 0f, 0.2f);
}
}
|