JAva j 2 me M 3 Gパッケージ使用

19863 ワード

importjavax.microedition.lcdui.Command;
importjavax.microedition.lcdui.CommandListener;
importjavax.microedition.lcdui.Display;
importjavax.microedition.lcdui.Displayable;
importjavax.microedition.lcdui.Graphics;
importjavax.microedition.lcdui.game.GameCanvas;
importjavax.microedition.m3g.Camera;
importjavax.microedition.m3g.Graphics3D;
importjavax.microedition.m3g.Loader;
importjavax.microedition.m3g.World;
importjavax.microedition.midlet.MIDlet;

publicclassRMMIDletextendsMIDletimplementsCommandListener{
privateDisplaymyDisplay=null;

privateRetainedCanvasmyCanvas=null;

privateCommandexitCommand=newCommand("Exit",Command.ITEM,1);

publicRMMIDlet(){
super();
myDisplay=Display.getDisplay(this);
myCanvas=newRetainedCanvas();
myCanvas.setCommandListener(this);
myCanvas.addCommand(exitCommand);
}

publicvoidstartApp(){
myCanvas.init();
myDisplay.setCurrent(myCanvas);
myCanvas.start();
}

publicvoidpauseApp(){
}

publicvoiddestroyApp(booleanunconditional){
myCanvas.stop();
}

publicvoidcommandAction(Commandcmd,Displayabledisp){
if(cmd==exitCommand){
try{
destroyApp(false);
notifyDestroyed();
}catch(Exceptione){
e.printStackTrace();
}
}
}

}

classRetainedCanvasextendsGameCanvasimplementsRunnable{
privatebooleanmRunning=false;

privateThreadmPaintThrd=null;

privateGraphics3DmGraphics3D=Graphics3D.getInstance();

privateWorldmWorld=null;

privateCameramCam=null;

privatelongmWorldStartTime=0;

publicRetainedCanvas(){
super(true);
}

publicvoidinit(){
try{
mWorld=(World)Loader.load("/p.m3g")[0];
mCam=mWorld.getActiveCamera();
mCam.translate(0,0,-1.5f);
mCam.setOrientation(180,0,1,0);
}catch(Exceptione){
e.printStackTrace();
}
mWorldStartTime=System.currentTimeMillis();

}

publicvoidstart(){
mRunning=true;
mPaintThrd=newThread(this);
mPaintThrd.start();
}

publicvoidstop(){
mRunning=false;
try{
mPaintThrd.join();
}catch(InterruptedExceptionex){
}
}

publicvoidrun(){
Graphicsg=getGraphics();
longstartTime;

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(InterruptedExceptionie){
}
}
}

privatevoidcameraForward(){
mCam.translate(0f,0f,0.2f);
}
}