?? m3gcanvas.java
字號(hào):
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.m3g.Background;
import javax.microedition.m3g.Camera;
import javax.microedition.m3g.Graphics3D;
import javax.microedition.m3g.Group;
import javax.microedition.m3g.Light;
import javax.microedition.m3g.Loader;
import javax.microedition.m3g.Node;
import javax.microedition.m3g.Object3D;
import javax.microedition.m3g.Transform;
import javax.microedition.m3g.World;
class M3GCanvas extends GameCanvas implements Runnable {
// Choose another Camera25,26
// static final int CAMERA_ID = 30;
// static final int CAMERA_ID = 50001;
private final int LIGHT_ID = 28;
private Camera camera = null;
private MobileCamera mCamera = null;
Graphics3D g3d = Graphics3D.getInstance();
World myWorld = null;
private Brick brick = new Brick();
private Block block = new Block();
private Node block1,block2;
private boolean isGameOver = false;
public M3GCanvas() {
super(false);
setFullScreenMode(false);
// System.out.println("Brick:" );
// System.out.println(new Group().toString());
try {
// Load the world from the m3g file
Object3D[] list = Loader.load("/gogo.m3g");
for (int i = 0; i < list.length; i++) {
if (list[i] instanceof World) {
myWorld = (World) list[i];
break;
}
}
// myWorld = (World) Loader.load("/gogo.m3g")[0];
// camera = (Camera) myWorld.find(CAMERA_ID);
// float[] temp = new float[16];
// Transform transform = new Transform();
// myWorld.getTransform(transform);
// transform.get(temp);
// for(int i = 0; i < 16; i++){
// System.out.print(temp[i]);
// if(i%4==3)
// System.out.println("\n");
//
// }
// Transform tf = new Transform();
// float[] temp2 = new float[]{
// 2,0,0,0,
// 0,2,0,0,
// 0,0,2,0,
// 0,0,0,2};
// tf.set(temp2);
// myWorld.setTransform(tf);
// myWorld = new World();
float viewport_width = getWidth();
float viewport_height = getHeight();
float aspectRatio = (float)viewport_width/(float)viewport_height;
mCamera = new MobileCamera(getWidth(), getHeight());
// Light light = (Light)myWorld.find(LIGHT_ID);
// Group group = new Group();
// while(myWorld.getChildCount() > 0){
// Node node = myWorld.getChild(0);
// myWorld.removeChild(node);
// group.addChild(node);
// }
myWorld.removeChild((Node)myWorld.find(31));
myWorld.removeChild((Node)myWorld.find(32));
// myWorld.addChild(group);
//
Background back = new Background();
back.setColor(0x49C6F6);
myWorld.setBackground(back);
// light.setMode(Light.OMNI);
// light.translate(0,2,0);
// light.setIntensity(0.5f);
// myWorld.addChild(light);
myWorld.addChild(mCamera.getCameraGroup());
// Group group1 = new Group();
// group1.addChild(brick.getBrick());
// myWorld.addChild(brick.getBrick());
block1 = block.getBlock();
// float[] temp = new float[16];
// Transform tf = new Transform();
// block1.getTransform(tf);
// tf.get(temp);
// for(int i=0;i<16;i++){
// System.out.print(temp[i]+",");
// if(i%4==3){
// System.out.println("\n");
// }
// }
//
// brick.getBrick().getTransform(tf);
// tf.get(temp);
// for(int i=0;i<16;i++){
// System.out.print(temp[i]+",");
// if(i%4==3){
// System.out.println("\n");
// }
// }
// block1.translate(5, 5, 0);
// block2 = (Node)block1.duplicate();
// block2.setOrientation(-45, 0, 0, 1);
// block2.translate(1, 0, 0);
// block2.translate(-5, -5, 0);
myWorld.addChild(block1);
// myWorld.addChild(block2);
myWorld = brick.createMap(myWorld);
camera = mCamera.getCamera();
// camera.setAlignment(brick.getBrick(), Node.ORIGIN, myWorld, Node.Y_AXIS);
// camera.setPerspective(45, aspectRatio,1.0f,1000.0f);
myWorld.setActiveCamera(camera);
} catch (Exception e) {
e.printStackTrace();
}
Thread thread = new Thread(this);
thread.start();
// paint();
}
public void paint(Graphics g2d) {
try {
if(isGameOver){
g2d.setColor(0);
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.drawString("GAME OVER", 50, 50, Graphics.LEFT|Graphics.TOP);
}
g3d.bindTarget(g2d); // Binds the given Graphics or mutable
// Image2D as the rendering target of this
// Graphics3D
g3d.clear(null);
g3d.render(myWorld);
} finally {
g3d.releaseTarget();
}
// flushGraphics();
}
// public void paint() {
// Graphics g2d = getGraphics();
// try {
// g3d.bindTarget(g2d); // Binds the given Graphics or mutable
// // Image2D as the rendering target of this
// // Graphics3D
// g3d.clear(null);
// g3d.render(myWorld);
// } finally {
// g3d.releaseTarget();
// }
// flushGraphics();
// }
protected void keyPressed(int keyCode) {
int gameAction = getGameAction(keyCode);
mCamera.pressedKey(gameAction);
block.pressedKey(keyCode);
}
protected void keyReleased(int keyCode) {
int gameAction = getGameAction(keyCode);
mCamera.releasedKey(gameAction);
block.releasedKey(keyCode);
}
public void run() {
// TODO Auto-generated method stub
while (true) {
mCamera.update();
if(block.checkImpact()){
isGameOver = true;
break;
}
// block.updateMove();
// System.out.println(mCamera.getPosition());
repaint();
}
repaint();
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -