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

📄 m3gcanvas.java

📁 Java ME手机应用开发大全一书的配套光盘上的源码
💻 JAVA
字号:
import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;import javax.microedition.m3g.*;class M3GCanvas extends GameCanvas implements Runnable{      private Graphics3D g3d; // 声明用来绘制世界的Graphics3D对象    private World world; //声明包含摄像机和金字塔多面体的世界对象     private Camera camera; //声明摄像机对象     private Mesh pyramidMesh; // 声明金字塔多面体对象        public M3GCanvas(){        super(false);        //设置全屏模式        setFullScreenMode(true);        //创建Graphics3D实例        g3d = Graphics3D.getInstance();		//创建世界实例        world = new World();        //创建摄像机实例        camera = new Camera();        world.addChild(camera); //将摄像机添加到世界中         float w = getWidth();        float h = getHeight();        // 设置摄像机为透视模式        camera.setPerspective(60.0f, w / h, 0.1f, 50f);        //创建金字塔多面体实例        pyramidMesh = createpyramid();         pyramidMesh.setTranslation(0.0f, 0.0f, -3.0f);         world.addChild(pyramidMesh); // 将金字塔多面体添加到世界中        //设置当前摄像机为场景的活动摄像机        world.setActiveCamera(camera);		//启动金字塔旋转的线程        Thread t = new Thread(this);        t.start();    }    //定义绘制3D场景的方法    public void draw3D(Graphics g){        try{            g3d.bindTarget(g); // 将Graphics对象绑定到Graphics3D对象上            g3d.render(world); // 绘制世界场景        }finally{            g3d.releaseTarget();        }    }    public void run() {        Graphics g = getGraphics();        while(true){            // 金字塔环绕Y轴旋转1度            pyramidMesh.postRotate(3.0f, 0.0f, 1.0f, 0.0f);            draw3D(g);            flushGraphics();        }    }    // 创建金字塔多面体的方法    private Mesh createpyramid(){          // 定义顶点数组要用到的顶点坐标        short []POINTS = new short[] {-1, -1, 1,                                       1, -1, 1,                                       1, -1, -1,                                       -1, -1, -1,                                       0, 1, 0};                                                                     // The points sequence.        int []INDICES = new int[] {0, 1, 4, 1, 2, 4, 2, 3, 4, 3, 0, 4, 2, 1, 0, 2, 0, 3};        byte []COLORS = new byte[] {127, 0, 0, //红色                                    0, 127, 0,  //绿色                                    0, 0, 127, //蓝色                                    127, 0, 127,                                     0, 127, 127};                                                                // 金字塔由6个三角带组成        int []LENGTH = new int[] {3, 3, 3, 3, 3, 3};         //声明顶点数组        VertexArray POSITION_ARRAY, COLOR_ARRAY;		//声明索引缓冲        IndexBuffer INDEX_BUFFER;        //创建顶点数组        POSITION_ARRAY = new VertexArray(POINTS.length / 3, 3, 2);        POSITION_ARRAY.set(0, POINTS.length / 3, POINTS);        COLOR_ARRAY = new VertexArray(COLORS.length / 3, 3, 1);        COLOR_ARRAY.set(0, COLORS.length / 3, COLORS);        INDEX_BUFFER = new TriangleStripArray(INDICES, LENGTH);		 //用顶点数组创建顶点缓冲        VertexBuffer vertexBuffer = new VertexBuffer();        vertexBuffer.setPositions(POSITION_ARRAY, 1.0f, null);        vertexBuffer.setColors(COLOR_ARRAY);                //创建3D多面体对象        Mesh mesh = new Mesh(vertexBuffer, INDEX_BUFFER, null);        //创建外观属性        Appearance appearance = new Appearance();         PolygonMode polygonMode = new PolygonMode();        polygonMode.setPerspectiveCorrectionEnable(true);        polygonMode.setCulling(PolygonMode.CULL_NONE);         polygonMode.setShading(PolygonMode.SHADE_SMOOTH);        appearance.setPolygonMode(polygonMode);        // 为3D多面体对象设置外观属性        mesh.setAppearance(0, appearance);         return mesh;    }    }

⌨️ 快捷键说明

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