J2ME 3D编程——第一个3D程序(附源代码)[Java编程]
本文“J2ME 3D编程——第一个3D程序(附源代码)[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
参考WTK2.2供应的demo,完成了第一个3D程序,固然很简单,并且有些问题还不是很清楚,还是把代码同享出来和乐意学习J2ME 3D编程的朋友一同学习.
关于3D编程的资料,可以查阅我的blog.
关于代码的编译和运行阐明以下:
1、以下代码在J2ME WTK2.2下面编译通过.
2、代码分为两个文件:First3DCanvas.java和First3DMIDlet.java.
3、利用J2ME WTK2.2成立新的工程,主MIDlet类为:first3d. First3DMIDlet
4、将代码保存在你的工程目录下的first3d目录下.
5、将J2ME WTK安装目录下的
appsDemo3D escomsuperscapem3gwtksamples etainedmodecontent目录中的swerve.m3g文件复制到你的工程目录下的res目录下.
6、你的工程成立后,设置工程,通过WTK界面中的“设置”按钮翻开设置窗口,在“API挑选”中,设置“目标平台”为:自定义;“简档”为“MIDP2.0”;“配置”为“CLDC1.1”;选中“Mobile 3D Graphics for J2ME(JSR184)”.
7、这样你便可以编译和运行以下代码了.
源代码以下:
// First3DMIDlet.java
package first3d;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class First3DMIDlet extends MIDlet {
private First3DCanvas displayable = new First3DCanvas();
public void startApp() {
Display.getDisplay(this).setCurrent(displayable);
}
public void pauseApp() {}
public void destroyApp(boolean unconditional) {}
}
// First3Dcanvas.java
package first3d;
import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;
import java.util.*;
/**
* 第一个3D程序
*/
public class First3DCanvas
extends Canvas
implements Runnable {
/**World对象*/
private World myWorld = null;
/**Graphics3D对象*/
private Graphics3D g3d = Graphics3D.getInstance();
/**Camera对象*/
private Camera cam = null;
private int viewport_x;
private int viewport_y;
private int viewport_width;
private int viewport_height;
private long worldStartTime = 0;
//重绘时间
private int validity = 0;
public First3DCanvas() {
//启动重绘界面的线程
Thread thread = new Thread(this);
thread.start();
try {
//导入3D图片
myWorld = (World) Loader.load("/swerve.m3g")[0];
viewport_x = 0;
viewport_y = 0;
viewport_width = getWidth();
viewport_height = getHeight();
cam = myWorld.getActiveCamera();
//设置cam对象
float[] params = new float[4];
int type = cam.getProjection(params);
if (type != Camera.GENERIC) {
//calculate window aspect ratio
float waspect = viewport_width / viewport_height;
if (waspect < params[1]) {
float height = viewport_width / params[1];
viewport_height = (int) height;
viewport_y = (getHeight() - viewport_height) / 2;
}
else {
float width = viewport_height * params[1];
viewport_width = (int) width;
viewport_x = (getWidth() - viewport_width) / 2;
}
}
worldStartTime = System.currentTimeMillis();
}
catch (Exception e) {}
}
protected void paint(Graphics g) {
//排除后台
g.setColor(0x00);
g.fillRect(0, 0, getWidth(), getHeight());
//和3D对象绑定
g3d.bindTarget(g);
g3d.setViewport(viewport_x, viewport_y, viewport_width, viewport_height);
long startTime = System.currentTimeMillis() - worldStartTime;
validity = myWorld.animate((int)startTime);
try {
g3d.render(myWorld);
}
finally {
g3d.releaseTarget();
}
}
public void run() {
try{
while(true){
//重画图形
repaint(viewport_x, viewport_y, viewport_width, viewport_height);
}
}catch(Exception e){}
}
}
以上是“J2ME 3D编程——第一个3D程序(附源代码)[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |