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

📄 m3gcanvas.java

📁 3D手机游戏开发实例源代码
💻 JAVA
字号:
import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;import javax.microedition.m3g.*;public class M3GCanvas extends GameCanvas implements Runnable {    private Graphics3D g3d ;    private World world;    private Camera camera;    private int iStartTime;    private Graphics g;    private int viewport_x;    private int viewport_y;    private int viewport_width;    private int viewport_height;    public M3GCanvas(){        super(true);         try {                world = (World)Loader.load("/tunnel.m3g")[0];                setupAspectRatio();         } catch (Exception e) {                e.printStackTrace();         }        iStartTime = (int)System.currentTimeMillis();        Thread t = new Thread(this);        t.start();    }    public void run() {        Graphics g = getGraphics();        while(true) {        world.animate((int) System.currentTimeMillis() - iStartTime);           g.setColor( 255, 255, 255 );        g.fillRect( 0, 0, getWidth(), getHeight() );         g3d = Graphics3D.getInstance();        g3d.bindTarget(g);        try{            g3d.render(world);         }catch(Exception e){            System.err.println(e);        }finally{            g3d.releaseTarget();        }        flushGraphics();            try {                Thread.sleep(200);             }            catch (InterruptedException e) {                            }        }    }    void setupAspectRatio() {        viewport_x = 0;        viewport_y = 0;        viewport_width = getWidth();        viewport_height = getHeight();        camera = world.getActiveCamera();        float[] params = new float[4];        int type = camera.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;            }        }    }}

⌨️ 快捷键说明

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