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

📄 simplespincolorcube.java

📁 人民邮电出版社的《J2ME手机开发入门》全部源代码
💻 JAVA
字号:
/*
 * SimpleSpinColorCube.java
 *
 * Created on 2004-11-23, 13:19
 */

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

public class SimpleSpinColorCube extends MIDlet {
    MyCanvas displayable = new MyCanvas();
    Timer iTimer = new Timer();
    
    public SimpleSpinColorCube() {
        //定义匿名类处理命令按钮事件
        displayable.setCommandListener(new CommandListener()  {
            public void commandAction(Command c, Displayable d) {
                if (c.getCommandType() == Command.EXIT) {
                    notifyDestroyed();
                }
            }
        });
    }
    
    public void startApp() {
        displayable.addCommand(new Command("Exit", Command.EXIT, 1));
        Display.getDisplay(this).setCurrent(displayable);
        iTimer.schedule( new RepaintTimer(), 0, 40 );
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
    
    class RepaintTimer extends TimerTask {
        public void run() {
            if( displayable != null ) {
                displayable.repaint();
            }
        }
    }
}

class MyCanvas extends Canvas {   
    //顶点坐标
    static final short[] cubeVerticesMatrix = {
        5, 5, 5,  -5, 5, 5,   5,-5, 5,  -5,-5, 5,   // 前面4个顶点
                -5, 5,-5,   5, 5,-5,  -5,-5,-5,   5,-5,-5,   // 后面4个顶点
                -5, 5, 5,  -5, 5,-5,  -5,-5, 5,  -5,-5,-5,   // 左面四个顶点
                5, 5,-5,   5, 5, 5,   5,-5,-5,   5,-5, 5,   // 右面四个顶点
                5, 5,-5,  -5, 5,-5,   5, 5, 5,  -5, 5, 5,   // 上面四个顶点
                5,-5, 5,  -5,-5, 5,   5,-5,-5,  -5,-5,-5    // 下面四个顶点
    };
    
    //顶点坐标颜色
    static final byte[] CUBE_COLORS = {
        0, 0,-127,    0, -127, 0,    -127, 0, 0,    0, 0,-127,
                0, 127, 127,    127, 127, 127,    0, 127, 0,    0, 127, 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,
                127, 0, 0,    0, 0, 127,    0, 127, 0,    0, 0, 127,
                0,-127, 0,    0,-127, 0,    0,-127, 0,    0,-127, 0
    };

    //每个三角形的长度
    static final int[] stripLen = { 4, 4, 4, 4, 4, 4 };
    
    private Graphics3D      g3d;
    private Camera          cam;
    private float           iAngle = 0.0f;
    private Transform       iTransform = new Transform();
    private VertexBuffer    vbCube;    
    private IndexBuffer     ibCube;  
    private VertexArray     colorArray;
    

    public MyCanvas() {
        g3d = Graphics3D.getInstance();
        
        //创建照相机
        cam = new Camera();
        cam.setPerspective( 60.0f,            
                (float)getWidth()/ (float)getHeight(),
                1.0f, 
                1000.0f );
        Transform transform = new Transform();
        transform.postTranslate(0.0f, 0.0f, 30.0f);
        g3d.setCamera(cam, transform);
        
        //创建VertexArray
        //在创建VertexBuffer时要使用VertexArray
        VertexArray vaCubeVertices = new VertexArray(
                cubeVerticesMatrix.length / 3, 3, 2);
        vaCubeVertices.set(0, cubeVerticesMatrix.length/3, cubeVerticesMatrix);
          VertexArray vaColor = new VertexArray(
                CUBE_COLORS.length / 3, 3, 1);
        vaColor.set(0, CUBE_COLORS.length/3, CUBE_COLORS);      
        
        //创建VertexBuffer对象
        vbCube = new VertexBuffer();
        vbCube.setPositions(vaCubeVertices, 1.0f, null);    //设置顶点坐标
        vbCube.setColors(vaColor);                           //设置颜色
        
        //以内部索引创建IndexBuffer
        ibCube = new TriangleStripArray( 0, stripLen );
    }
    
    
    protected void paint(Graphics g) {
        try {
            g3d.bindTarget(g, true, Graphics3D.DITHER | Graphics3D.TRUE_COLOR);
            
            //清除缓冲
            g3d.clear(null);
            
            //旋转物体
            iAngle += 1.0f;
            iTransform.setIdentity();
            iTransform.postRotate(iAngle,       //旋转1度
                    1.0f, 1.0f, 1.0f);  //旋转环绕的轴
            
            g3d.render(vbCube, ibCube, new Appearance(), iTransform);
        } finally {
            g3d.releaseTarget();
        }
    }
}



⌨️ 快捷键说明

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