手机上J2ME的3D编程-简单创建3D立方体(2)
来源:互联网 发布:voip网络电话交换机 编辑:程序博客网 时间:2024/04/28 08:39
下面是源代码。介绍在手机上J2ME的3D编程-简单创建3D立方体(1)里面
从Midlet派生的类,是主文件
/*
* Created on 2005-3-23
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Box;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class BoxTest extends MIDlet implements Runnable, CommandListener {
private Display display;
private BoxCanvas boxCanvas = new BoxCanvas();
private Command cmdExit = new Command("Exit", Command.EXIT, 1);
private Command cmdRotate = new Command("Rotate", Command.ITEM, 1);
private Thread thread = null;
/**
*
*/
public BoxTest() {
super();
// TODO Auto-generated constructor stub
}
/* (non-Javadoc)
* @see javax.microedition.midlet.MIDlet#startApp()
*/
protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub
display = Display.getDisplay(this);
display.setCurrent(boxCanvas);
boxCanvas.addCommand(cmdExit);
boxCanvas.addCommand(cmdRotate);
boxCanvas.setCommandListener(this);
boxCanvas.CreateWorld();
}
/* (non-Javadoc)
* @see javax.microedition.midlet.MIDlet#pauseApp()
*/
protected void pauseApp() {
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
*/
protected void destroyApp(boolean arg0){
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
// TODO Auto-generated method stub
while(true){
boxCanvas.Rotate();
}
}
/* (non-Javadoc)
* @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
*/
public void commandAction(Command c, Displayable d) {
// TODO Auto-generated method stub
if(c == cmdExit){
destroyApp(true);
notifyDestroyed();
} else if(c == cmdRotate){
if(thread == null){
thread = new Thread(this);
thread.start();
}
}
}
}
从Canvas派生的类
/*
* Created on 2005-3-23
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Box;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.m3g.Camera;
import javax.microedition.m3g.Graphics3D;
import javax.microedition.m3g.Mesh;
import javax.microedition.m3g.World;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class BoxCanvas extends Canvas {
private Camera camera;
private World world;
private Graphics3D g3d = null;
private BoxModel boxModel = new BoxModel();
BoxCanvas(){
g3d = Graphics3D.getInstance();
}
/* (non-Javadoc)
* @see javax.microedition.lcdui.Canvas#paint(javax.microedition.lcdui.Graphics)
*/
protected void paint(Graphics g) {
// TODO Auto-generated method stub
//Mesh m = boxModel.GetMesh();
//m.translate(0,0, -1);
Draw(g);
}
public void CreateWorld(){
setFullScreenMode(true);
//create a camera
camera = new Camera();
camera.setPerspective(60.0f, // field of view
(float) getWidth() / (float) getHeight(), // aspectRatio
1.0f, // near clipping plane
1000.0f); // far clipping plane
camera.setTranslation(0,0, 50);
world = new World();
world.addChild(camera);
boxModel.setModel(20,20,20);
boxModel.setImageUrl("/texture.png");
boxModel.CreateModel();
world.addChild(boxModel.GetMesh());
world.setActiveCamera(camera);
//System.out.println("CreateWorld Sucess!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
repaint();
}
private void Draw(Graphics g){
try{
g3d.bindTarget(g);
g3d.render(world);
}catch(Exception e){
}finally{
g3d.releaseTarget();
}
}
/**
*
*/
public void Rotate() {
// TODO Auto-generated method stub
Mesh m = boxModel.GetMesh();
m.postRotate(1.0f, 1.0f, 0f, 0f);
repaint();
}
}
自定义的类,负责立方体的创建工作
/*
* Created on 2005-3-23
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package Box;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.m3g.Appearance;
import javax.microedition.m3g.Image2D;
import javax.microedition.m3g.Mesh;
import javax.microedition.m3g.PolygonMode;
import javax.microedition.m3g.Texture2D;
import javax.microedition.m3g.TriangleStripArray;
import javax.microedition.m3g.VertexArray;
import javax.microedition.m3g.VertexBuffer;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class BoxModel implements Runnable{
//Box attribute
private int boxLength = 0;
private int boxWidth = 0;
private int boxHigh = 0;
//
private VertexBuffer boxVB;
private Mesh mesh;
//
private String imageUrl = "";
private Image texImage = null;
private Texture2D texture=null;
private int getPlus(int i){
if(i < 0){
i = -i;
}
return i;
}
//设置立方体的长、宽、高
public void setModel(int l, int w, int h){
setLength(l);
setWidth(w);
setHigh(h);
}
public void setLength(int l){
this.boxLength = getPlus(l);
}
public void setWidth(int w){
this.boxWidth = getPlus(w);
}
public void setHigh(int h){
this.boxHigh = getPlus(h);
}
public void setImageUrl(String url){
this.imageUrl = url;
}
public void setImage(Image img){
this.texImage = img;
}
public void CreateModel(){
CreateMesh();
}
private void CreateMesh(){
short x = (short) (this.boxLength / 2);
short y = (short) (this.boxWidth / 2);
short z = (short) (this.boxHigh / 2);
short fx = (short) -x;
short fy = (short) -y;
short fz = (short) -z;
short[] vert = {x,y,z, fx,y,z, x,fy,z, fx,fy,z, //front
fx,y,fz, x,y,fz, fx,fy,fz, x,fy,fz, //back
fx,y,z, fx,y,fz, fx,fy,z, fx,fy,fz, //left
x,y,fz, x,y,z, x,fy,fz, x,fy,z, //right
x,y,fz, fx,y,fz, x,y,z, fx,y,z, //top
x,fy,z, fx,fy,z, x,fy,fz, fx,fy,fz}; //buttom
VertexArray vertArray = new VertexArray(vert.length/3, 3, 2);
vertArray.set(0, vert.length/3, vert);
byte[] norm = { 0,0,127, 0,0,127, 0,0,127, 0,0,127,
0,0,-127, 0,0,-127, 0,0,-127, 0,0,-127,
-127,0,0, -127,0,0, -127,0,0, -127,0,0,
127,0,0, 127,0,0, 127,0,0, 127,0,0,
0,127,0, 0,127,0, 0,127,0, 0,127,0,
0,-127,0, 0,-127,0, 0,-127,0, 0,-127,0};
VertexArray normArray = new VertexArray(norm.length / 3, 3, 1);
normArray.set(0, norm.length / 3, norm);
short[] tex = { 1, 0, 0, 0, 1, 1, 0, 1,
1, 0, 0, 0, 1, 1, 0, 1,
1, 0, 0, 0, 1, 1, 0, 1,
1, 0, 0, 0, 1, 1, 0, 1,
1, 0, 0, 0, 1, 1, 0, 1,
1, 0, 0, 0, 1, 1, 0, 1 };
VertexArray texArray = new VertexArray(tex.length / 2, 2, 2);
texArray.set(0, tex.length / 2, tex);
// create the VertexBuffer for our object
VertexBuffer vb = boxVB = new VertexBuffer();
vb.setPositions(vertArray, 1.0f, null);
vb.setNormals(normArray);
vb.setTexCoords(0, texArray, 1.0f, null);
// the length of each triangle strip
int[] stripLen = { 4, 4, 4, 4, 4, 4 };
// create the index buffer for our object (this tells how to
// create triangle strips from the contents of the vertex buffer).
TriangleStripArray tsa = new TriangleStripArray(0, stripLen);
//创建多边形模式。
PolygonMode pm = new PolygonMode();
pm.setShading(PolygonMode.SHADE_SMOOTH);
pm.setCulling(PolygonMode.CULL_NONE);
//生成外貌。
Appearance app = new Appearance();
app.setPolygonMode(pm);
LoadImage();
Image texImg= this.texImage;
Texture2D texture=null;
try{
// create texture from loaded image.
texture = new Texture2D(new Image2D(Image2D.RGB, this.texImage));
}catch(Exception e){
e.printStackTrace();
String strErr = "Failed to create texture Use Format --- Image2D.RGB";
System.out.println(strErr);
}
try{
// repeat texture on surface
texture.setWrapping(Texture2D.WRAP_REPEAT, Texture2D.WRAP_REPEAT);
}catch(Exception e){
e.printStackTrace();
System.out.println("Failed to create texture3");
}
try{
// Blend mode to use.
texture.setBlending(Texture2D.FUNC_DECAL);
}catch(Exception e){
System.out.println("Failed to create texture4");
}
try{
// Use nearest for performance, linear for quality
texture.setFiltering(Texture2D.FILTER_NEAREST,Texture2D.FILTER_NEAREST);
}catch(Exception e){
System.out.println("Failed to create texture5");
}
try{
app.setTexture(0, texture);
}catch(Exception e){
System.out.println("Failed to create texture6");
}
//创建网格。
mesh = new Mesh(vb, tsa, app);
mesh.setAppearance(0, app);
//mesh.translate(0.0f, 0.0f, -5.0f);
}
/**
*
*/
private void SetTexture2D() {
// TODO Auto-generated method stub
int[] format = {Image2D.ALPHA,
Image2D.LUMINANCE,
Image2D.LUMINANCE_ALPHA,
Image2D.RGB,
Image2D.RGBA};
for(int i=0; i<format.length;i++){
try{
// create texture from loaded image.
this.texture = new Texture2D(new Image2D(format[i], this.texImage));
}catch(Exception e){
e.printStackTrace();
String strErr = "Failed to create texture Use Format --- ";
strErr += i;
System.out.println(strErr);
continue;
}
break;
}
}
//调用图片
private void LoadImage(){
if(this.texImage != null){
return;
}
try{
this.texImage = Image.createImage(this.imageUrl);
}catch(Exception e){
String strErr="Load Image (path=";
strErr += this.imageUrl;
strErr += ") Fail!";
System.err.println(strErr);
this.texImage = CreateImage();
}
}
/**
*
*/
private Image CreateImage() {
// TODO Auto-generated method stub
Image image = null;
image = Image.createImage(this.boxWidth, this.boxLength);
//DrawDefaultImage(image);
Graphics g = image.getGraphics();
g.setColor(0xffff00);
g.fillRect(0,0,image.getWidth(), image.getHeight());
return image;
}
/**
* @param g
*/
public void DrawDefaultImage(Image img) {
// TODO Auto-generated method stub
Graphics g = img.getGraphics();
g.setColor(0xffff00);
g.fillRect(0,0,img.getWidth(), img.getHeight());
}
public Mesh GetMesh(){
//System.out.println("GetMesh!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
return this.mesh;
}
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
// TODO Auto-generated method stub
//CreateMesh();
//System.out.println("CreateModel Sucess!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
}
- 手机上J2ME的3D编程-简单创建3D立方体(2)
- 手机上J2ME的3D编程-简单创建3D立方体(1)
- 手机上J2ME的3D编程
- css3实践—创建3D立方体
- css3实践—创建3D立方体
- css3实践—创建3D立方体
- J2ME 3D编程的一些资料
- J2ME 3D编程的一些资料
- 一个简单的3D立方体C++源码
- 3D 旋转立方体
- 3D立方体(代码)
- 动态3D立方体(OpenGL图形编程)
- iOS CALayer和3D 定义一个简单的旋转3D立方体
- iOS CALayer和3D (1): 定义一个简单的旋转3D立方体
- 为基于J2ME的手机开发移动3D游戏
- 基于J2ME的手机开发移动3D游戏
- 为基于J2ME的手机开发移动3D游戏
- 为基于J2ME的手机开发移动3D游戏
- tomcat5.5+apache2.配置可以参考这个
- JAVA上加密算法的实现用例
- 多年开发Oracle,积累的Oracle基础函数库。
- 3-28-17:19tomcat5.5+apache2整合成功
- 如何成为一名C++程序员
- 手机上J2ME的3D编程-简单创建3D立方体(2)
- vb.net 也实现窗体淡淡出现
- 手段
- 手机上J2ME的3D编程-简单创建3D立方体(1)
- 用composite模式写的一个二叉树的例子
- >+ ASP六大对象常用语句示范 (继续添加中......)
- H.264 picture parameter sets成员值含义学习笔记
- SWT-JFace-Eclipse的例程小程序代码
- Linux下的多线程编程