?? simplespincolorcube.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 + -