扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:中国IT实验室 来源:中国IT实验室 2007年9月22日
关键字:
在本页阅读全文(共2页)
列表5. 完整的例程代码
package com.kontio;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.lang.IllegalArgumentException;
import java.io.*;
import java.util.*;
import javax.microedition.m3g.*;
public class Example3D extends MIDlet implements CommandListener{
//我们在场景中使用的对象的UserID
static final int POGOROO = 554921620;
static final int CAMERA = 769302310;
static final int TRANSFORM = 347178853;
static final int ROO = 418071423;
private Display myDisplay = null;
private ExampleCanvas myCanvas = null;
private Timer myRefreshTimer = new Timer();
private TimerTask myRefreshTask = null;
private Command exitCommand = new Command("Exit", Command.ITEM, 1);
Graphics3D myGraphics3D = Graphics3D.getInstance();
World myWorld = null;
private AnimationController animRoo = null;
private Group tRoo = null;
private Group tCams = null;
private Group acRoo = null;
private int animLength = 0;
int viewport_x;
int viewport_y;
int viewport_width;
int viewport_height;
public Example3D(){
super();
myDisplay = Display.getDisplay(this);
myCanvas = new ExampleCanvas(this);
myCanvas.setCommandListener(this);
myCanvas.addCommand(exitCommand);
}
public void startApp() throws MIDletStateChangeException{
myDisplay.setCurrent(myCanvas);
try{
// 从文件中加载World
myWorld = (World)Loader.load("/pogoroo.m3g")[0];
getObjects();
setupAspectRatio();
}
catch(Exception e){
e.printStackTrace();
}
myRefreshTask = new RefreshTask();
// 调度一个重要执行的计时器以显示出帧速率20fps.
myRefreshTimer.schedule(myRefreshTask, 0, 50);
}
void setupAspectRatio(){
viewport_x = 0;
viewport_y = 0;
viewport_width = myCanvas.getWidth();
viewport_height = myCanvas.getHeight();
Camera cam = myWorld.getActiveCamera();
float[] params = new float[4];
int type = cam.getProjection(params);
if(type != Camera.GENERIC){
//计算窗口的宽高比例
float waspect=viewport_width/viewport_height;
if (waspect
float height = viewport_width/params[1];
viewport_height=(int)height;
viewport_y=(myCanvas.getHeight()-viewport_height)/2;
}
else{
float width = viewport_height*params[1];
viewport_width=(int)width;
viewport_x=(myCanvas.getWidth()-viewport_width)/2;
}
}
}
public void getObjects(){
try{
tRoo = (Group) myWorld.find(POGOROO);
tCams = (Group) myWorld.find(CAMERA);
acRoo = (Group) myWorld.find(TRANSFORM);
animRoo = (AnimationController) myWorld.find(ROO);
//取得动画的长度
AnimationTrack track = acRoo.getAnimationTrack(0);
animLength = 1000; // 缺省的长度,1秒
if (track != null){
KeyframeSequence ks = track.getKeyframeSequence();
if (ks != null)
animLength = ks.getDuration();
}
}
catch(Exception e){
e.printStackTrace();
}
}
public void pauseApp(){}
public void destroyApp(boolean unconditional) throws
MIDletStateChangeException{
myRefreshTimer.cancel();
myRefreshTimer = null;
myRefreshTask = null;
}
public void paint(Graphics g){
if(g.getClipWidth() != viewport_width ||
g.getClipHeight() != viewport_height ||
g.getClipX() != viewport_x ||
g.getClipY() != viewport_y){
g.setColor(0x00);
g.fillRect(0, 0, myCanvas.getWidth(), myCanvas.getHeight());
}
if ((myGraphics3D != null) && (myWorld != null)){
myGraphics3D.bindTarget(g);
myGraphics3D.setViewport(viewport_x, viewport_y, viewport_width, viewport_height);
myGraphics3D.render(myWorld);
myGraphics3D.releaseTarget();
}
}
public void commandAction(Command cmd, Displayable disp)
{
if (cmd == exitCommand){
try{
destroyApp(false);
notifyDestroyed();
}
catch(Exception e){
e.printStackTrace();
}
}
}
private class RefreshTask extends TimerTask{
public void run(){
if(myCanvas !=null && myGraphics3D != null && myWorld != null{
int startTime = (int)System.currentTimeMillis();
int validity = myWorld.animate(startTime);
myCanvas.repaint(viewport_x, viewport_y,
viewport_width, viewport_height);
}
}
}
class ExampleCanvas extends Canvas{
Example3D myRooMIDlet;
int i = 0;
ExampleCanvas(Example3D Testlet) { myRooMIDlet = Testlet; }
void init() { }
void destroy() { }
protected void paint(Graphics g) { myRooMIDlet.paint(g); }
protected void keyPressed(int i) { }
protected void keyReleased(int i) { }
protected void keyRepeated(int i) { }
protected void pointerDragged(int x, int y) { }
protected void pointerPressed(int x, int y) { }
protected void pointerReleased(int x, int y) { }
}
}
九、运行在模拟器中的例程
图1展示了例程在WTK模拟器中运行的结果。图中的袋鼠和田地看上去棒极了。如果设计者选择对其中任何对象改变一下的话,可以用the3D工具来完成,而在例程MIDlet中不需要作任何变化。
图1 例程在模拟器中运行的结果
十、结论
现在,你又看到一种使用JSR 184(也称移动3D API)的更高级的方式来创建3D应用程序。在保留模式下,设计者可以使用现有的3D建模工具来创建3D世界和其中的对象,然后把这些模型输出到M3G文件中。之后,应用程序只需装入该模型并在屏幕上绘制3D世界的视图即可。
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者