📄 coordinatescanvas.java
字号:
/***************************************************************************** Description: The Canvas class. Used to render the graphics to the screen and to check key input. Created By: Oscar Vivall 2006-06-12 @file CoordinatesCanvas.java COPYRIGHT All rights reserved Sony Ericsson Mobile Communications AB 2004. The software is the copyrighted work of Sony Ericsson Mobile Communications AB. The use of the software is subject to the terms of the end-user license agreement which accompanies or is included with the software. The software is provided "as is" and Sony Ericsson specifically disclaim any warranty or condition whatsoever regarding merchantability or fitness for a specific purpose, title or non-infringement. No warranty of any kind is made in relation to the condition, suitability, availability, accuracy, reliability, merchantability and/or non-infringement of the software provided herein. *****************************************************************************/import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;import javax.microedition.m3g.*;import javax.microedition.m3g.Camera;public class CoordinatesCanvas extends GameCanvas implements Runnable { private Graphics3D g3d; // object to do the rendering. private World world; // the world to put our figures in. private Camera camera; private Background bg; private Mesh mesh; // this is the Arrow figure private Transform transform; private Transform localTransform; private Mesh worldCoord; // the world coordinate figure. private Mesh localCoord; // the local coordinate figure. private final int WIDTH, HEIGHT; // canvas size. // used to display a info text for a few seconds private String info = ""; private long startTime = 0; private long timer = 0; private MIDlet midlet; public CoordinatesCanvas(MIDlet m){ super(false); setFullScreenMode(true); midlet = m; g3d = Graphics3D.getInstance(); world = new World(); camera = new Camera(); world.addChild(camera); bg = new Background(); bg.setColor(0x00); world.setBackground(bg); WIDTH = getWidth();; HEIGHT = getHeight(); camera.setPerspective(60.0f, (float)WIDTH / (float)HEIGHT, 0.1f, 500f); camera.translate(0.0f, 20.0f, 50.0f); camera.postRotate(-15.0f, 1.0f, 0.0f, 0.0f); mesh = MeshTool.createMesh(); mesh.translate(0.0f, 0.0f, 0.0f); world.addChild(mesh); worldCoord = MeshTool.createPlane((short)50); worldCoord.translate(0.0f, -25.0f, -40.0f); world.addChild(worldCoord); localCoord = MeshTool.createPlane((short)10); world.addChild(localCoord); transform = new Transform(); transform.setIdentity(); localTransform = new Transform(); localTransform.setIdentity(); world.setActiveCamera(camera); new Thread(this).start(); } public void draw3D(Graphics graphics){ try{ g3d.bindTarget(graphics); g3d.render(world); }catch(Exception e){ e.printStackTrace(); }finally{ g3d.releaseTarget(); } } public void draw2D(Graphics g){ g.setColor(0xFFFFFF); g.drawString(info, 0, 0, 0); g.setColor(0xFF0000); g.drawString("Help...", 0,HEIGHT, Graphics.LEFT | Graphics.BOTTOM); g.drawString("Exit", WIDTH ,HEIGHT, Graphics.RIGHT | Graphics.BOTTOM); } /* * When a key is pressed the main figure (Arrow) is moved or rotated. * The mesh illustrating the world coordinates iws not moved. * The mesh illustrating the local coordinates is rotated with the main mesh and moved in the world. * * Note that different methods are used to rotate or translate the figures. * When the figure is rotated the local coordinate system is also rotated. * * Keys: * 7, 9 - Move the mesh on the Z-axis using world coordinates. * 9, # - Move the mesh on the Z-axis using local coordinates. * Joy pad - Rotate the mesh around the center of the local coordinate system. * 4,6 - Rotate the mesh around it's centre */ public void keyPressed(int key){ System.out.println(key); startTime = System.currentTimeMillis()/1000; switch(key){ case -6: // left soft button // Display the help screen. JSR184Coordinates j = (JSR184Coordinates)midlet; j.showHelp(this); break; case -7: // right soft button // exit the midlet midlet.notifyDestroyed(); break; case -1: // up mesh.postRotate(3.0f, 1.0f, 0.0f, 0.0f); localCoord.postRotate(3.0f, 1.0f, 0.0f, 0.0f); info = "Mesh Rotate +X-axis"; break; case -2:// down mesh.postRotate(-3.0f, 1.0f, 0.0f, 0.0f); localCoord.postRotate(-3.0f, 1.0f, 0.0f, 0.0f); info = "Mesh Rotate -X-axis"; break; case -3: // left mesh.postRotate(3.0f, 0.0f, 1.0f, 0.0f); localCoord.postRotate(3.0f, 0.0f, 1.0f, 0.0f); info = "Mesh Rotate +Y-axis"; break; case -4: // right mesh.postRotate(-3.0f, 0.0f, 1.0f, 0.0f); localCoord.postRotate(-3.0f, 0.0f, 1.0f, 0.0f); info = "Mesh Rotate -Y-axis"; break; case 50: // 2 transform.postRotate(3.0f, 1.0f, 0.0f, 0.0f); localTransform.postRotate(3.0f, 1.0f, 0.0f, 0.0f); mesh.setTransform(transform); localCoord.setTransform(localTransform); info = "Transform Rotate +X-axis"; break; case 56: // 8 transform.postRotate(-3.0f, 1.0f, 0.0f, 0.0f); localTransform.postRotate(-3.0f, 1.0f, 0.0f, 0.0f); mesh.setTransform(transform); localCoord.setTransform(localTransform); info = "Transform Rotate -X-axis"; break; case 52: // 4 transform.postRotate(3.0f, 0.0f, 1.0f, 0.0f); localTransform.postRotate(3.0f, 0.0f, 1.0f, 0.0f); mesh.setTransform(transform); localCoord.setTransform(localTransform); info = "Transform Rotate +Y-axis"; break; case 54: // 6 transform.postRotate(-3.0f, 0.0f, 1.0f, 0.0f); localTransform.postRotate(-3.0f, 0.0f, 1.0f, 0.0f); mesh.setTransform(transform); localCoord.setTransform(localTransform); info = "Transform Rotate -Y-axis"; break; case 55: // 7 mesh.translate(0.0f, 0.0f, -5.0f); localCoord.translate(0.0f, 0.0f, -5.0f); info = "Mesh Translate -Z-axis"; break; case 42: // * mesh.translate(0.0f, 0.0f, 5.0f); localCoord.translate(0.0f, 0.0f, 5.0f); info = "Mesh Translate +Z-axis"; break; case 57: // 9 transform.postTranslate(0.0f, 0.0f, -5.0f); mesh.setTransform(transform); info = "Transform Translate -Z-axis"; break; case 35: // # transform.postTranslate(0.0f, 0.0f, 5.0f); mesh.setTransform(transform); info = "Transform Translate +Z-axis"; break; } } public void keyRepeated(int key){ keyPressed(key); } public void run() { Graphics graphics = getGraphics(); while(true){ timer = System.currentTimeMillis()/1000; if(timer - startTime > 3){ info = ""; } draw3D(graphics); draw2D(graphics); flushGraphics(); try{ Thread.sleep(10); }catch(Exception e){} } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -