⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simple3dexample.java

📁 java 3d 手机游戏 开发环境Eclipse
💻 JAVA
字号:
/*
 * 简单3D例子
 */

import java.io.IOException;

import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;
import javax.microedition.midlet.*;

public class Simple3DExample extends MIDlet implements CommandListener {

	private Command exitCommand;

	private Simple3DCanvas simple3D;

	public Simple3DExample() {
		exitCommand = new Command("Exit", Command.EXIT, 1);
		simple3D = new Simple3DCanvas();
		simple3D.addCommand(exitCommand);
		simple3D.setCommandListener(this);
		Display.getDisplay(this).setCurrent(simple3D);

	}

	protected void startApp() {
	}

	protected void pauseApp() {
	}

	protected void destroyApp(boolean arg0) {
		simple3D = null;
	}

	public void commandAction(Command c, Displayable d) {
		if (c == exitCommand) {
			destroyApp(false);
			notifyDestroyed();
		}
	}
}

class Simple3DCanvas extends Canvas implements Runnable {

	private Graphics3D g3d;

	private World world = null;

	private int validity;

	private boolean canplay = false;

	private Thread thread;

	public Simple3DCanvas() {

		g3d = Graphics3D.getInstance();
		try {
			Object root[] = Loader.load("/nokia_on_ice.m3g");
			world = (World) root[0];			
			
		} catch (IOException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}
		thread = new Thread();
		thread.start();
	}

	protected void paint(Graphics g) {

		// Get the current time.
		long currentTime = System.currentTimeMillis();
		validity = world.animate((int) currentTime);
		g3d.bindTarget(g);
		g3d.render(world);
		g3d.releaseTarget();

		//获得播放一帧3d动画所需要的时间
		validity -= System.currentTimeMillis() - currentTime;

		if (validity < 1) {
			//如果时间小于1ms,则设置维1ms.
			validity = 1;
		}
		if (validity < 0x7fffffff) {
			//控制每帧播放的间隔时间
			try {
				Thread.sleep(validity);
			} catch (InterruptedException e) {				
				e.printStackTrace();
			}

			repaint();
		}

	}

	public void run() {
		
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -