📄 m3gcanvas.java
字号:
import javax.microedition.lcdui.*;
import javax.microedition.m3g.*;
import javax.microedition.lcdui.game.*;
public class M3GCanvas extends GameCanvas implements Runnable
{
private Command exitCmd;
private Graphics3D g3d;
private Graphics g2d;
private World world;
private Image2D image2D;
private MobileCamera mobileCamera;
private Group cameraGroup;
private Tree tree1, tree2, tree3; // tree billboards
public M3GCanvas()
{ super(false);
g3d = Graphics3D.getInstance();
world = new World(); // create scene graph
buildScene();
Thread t = new Thread(this);
t.start();
// start the animation
} // end of SkinCanvas()
public void run() {
g2d = getGraphics();
while(true){
try{
g3d.bindTarget(g2d); // Binds the given Graphics or mutable Image2D as the rendering target of this Graphics3D
//g3d.clear(null);
g3d.render(world);
}finally{
g3d.releaseTarget();
}
mobileCamera.update();
tree1.align(); // align the billboards with the camera position
tree2.align();
tree3.align();
g2d.setColor(255,255,255);
g2d.drawString( mobileCamera.getKeyMode(), 5,5, Graphics.TOP|Graphics.LEFT);
g2d.drawString( mobileCamera.getPosition(), 5,18, Graphics.TOP|Graphics.LEFT);
g2d.drawString( mobileCamera.getRotation(), 5,31, Graphics.TOP|Graphics.LEFT);
flushGraphics();
}
}
private void buildScene()
/* Add a background, camera, trees, the bending model,
and a floor to the scene. There are no lights needed,
since nothing uses a Material component.
*/
{ /* The ordering of the first three methods is important.
The Billboard trees need a reference to the camera, and
the camera needs a reference to the background. */
addCamera();
addTrees();
addFloor();
} // end of buildScene()
private void addCamera()
{
mobileCamera = new MobileCamera(getWidth(),getHeight());
cameraGroup = mobileCamera.getCameraGroup();
world.addChild( cameraGroup );
world.setActiveCamera( mobileCamera.getCamera() );
} // end of addCamera()
private void addTrees()
{
image2D = loadImage("/tree1.png");
tree1 = new Tree(image2D, cameraGroup, 1.5f, 0, 2.0f);
// needs a ref. to cameraGroup
world.addChild( tree1.getMesh() );
image2D = loadImage("/tree2.png");
tree2 = new Tree(image2D, cameraGroup, -2.0f, -2.0f, 1.5f);
// needs a ref. to cameraGroup
world.addChild( tree2.getMesh() );
image2D = loadImage("/tree3.png");
tree3 = new Tree(image2D, cameraGroup, 0.5f, -3.2f, 1.5f);
// needs a ref. to cameraGroup
world.addChild( tree3.getMesh() );
} // end of addTrees()
private void addFloor()
{
image2D = loadImage("/grass.png");
TiledFloor floor = new TiledFloor( image2D, 8);
// 8 by 8 size, made up of 1 by 1 tiles
world.addChild( floor.getFloorMesh() ); // add the floor
} // end of addFloor()
private Image2D loadImage(String filename)
{
try {
image2D = (Image2D)Loader.load(filename)[0];
}
catch (Exception e)
{ System.out.println("Cannot make image from " + filename); }
return image2D;
}
// ------------ process key presses and releases ------------
protected void keyPressed(int keyCode)
{ int gameAction = getGameAction(keyCode);
mobileCamera.pressedKey(gameAction);
}
protected void keyReleased(int keyCode)
{ int gameAction = getGameAction(keyCode);
mobileCamera.releasedKey(gameAction);
}
// ------------------ update and paint the canvas ------------------
} // end of SkinCanvas class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -