J2ME 3D学习笔记——实现简单的界面(附代码)

//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中找到

你可能感兴趣的:(UP)