//MIDlet代碼
import Javax.microedition.lcdui.*;
import Javax.microedition.midlet.*;
public class Test3DMIDlet extends MIDlet{
M3GCanvas m3g = new M3GCanvas();
public void startApp(){
Display.getDisplay(this).setCurrent(m3g);
}
public void pauseApp(){}
public void destroyApp(boolean b){}
}
//Canvas代碼
import Javax.microedition.lcdui.*;
import Javax.microedition.m3g.*;
public class M3GCanvas extends Canvas{
/**World對象*/
private World world;
/**Graphics3D對象*/
Graphics3D g3d;
/**Camera對象*/
Camera camera;
public M3GCanvas(){
try{
//導入圖片
Object3D[] obj = Loader.load("/res/pogoroo.m3g");
//獲得圖片中的World對象
for(int i = 0;i < obj.length;i++){
if(obj[i] instanceof World){
world = (World)obj[i];
break;
}
}
//獲得Graphics3D對象
g3d = Graphics3D.getInstance();
//獲得獲得Camera
camera = world.getActiveCamera();
}catch(Exception e){
System.out.println(e);
}
}
public void paint(Graphics g){
try{
//綁定
g3d.bindTarget(g);
//代理
g3d.render(world);
}catch(Exception e){
}finally{
//釋放
g3d.releaseTarget();
}
}
public void keyPressed(int keyCode){
int action = this.getGameAction(keyCode);
if(action == Canvas.UP){
//移動相機
camera.setTranslation(0,0,0.2f);
}
//重新繪制
repaint();
}
}
注:其中pogoroo.m3g文件可以從WTK22的\aPPS\Demo3D中找到