📄 boxmidlet.java
字号:
import java.util.Timer;
import java.util.TimerTask;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
import javax.microedition.m3g.Camera;
import javax.microedition.m3g.Graphics3D;
import javax.microedition.m3g.Light;
import javax.microedition.m3g.Loader;
import javax.microedition.m3g.World;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class BoxMidlet extends MIDlet implements CommandListener {
// UserIDs for objects in the teapot model.
// static final int TEAPOT_ID = 16;
static final int CAMERA_ID = 10;
// Choose another Camera
// static final int CAMERA_ID = 25;
private Display myDisplay = null;
private BoxCanvas myCanvas = null;
private TimerTask myRefreshTask = null;
private Camera camera = null;
private Timer myRefreshTimer = new Timer();
private Command exitCommand = new Command("Exit", Command.ITEM, 1);
Graphics3D myGraphics3D = Graphics3D.getInstance();
// Contains our entire scene graph
World myWorld = null;
int viewport_x;
int viewport_y;
int viewport_width;
int viewport_height;
/**
* BoxMidlet - default constructor.
*/
public BoxMidlet() {
super();
// Set up the user interface.
myDisplay = Display.getDisplay(this);
myCanvas = new BoxCanvas(this);
myCanvas.setCommandListener(this);
myCanvas.addCommand(exitCommand);
}
/**
* startApp()
*/
public void startApp() throws MIDletStateChangeException {
myDisplay.setCurrent(myCanvas);
try {
// Load the world from the m3g file
myWorld = (World) Loader.load("/box.m3g")[0];
getObjects();
//loadLight();
setupAspectRatio();
} catch (Exception e) {
e.printStackTrace();
}
myRefreshTask = new RefreshTask();
// Schedule a repeating timer with a frame rate of 20fps.
myRefreshTimer.schedule(myRefreshTask, 0, 50);
}
private void loadLight(){
try {
Light light = new Light();
// Set light to Ambient
light.setMode(Light.AMBIENT);
// Set light intensity
light.setIntensity(1.0f);
// Add light to world
myWorld.addChild(light);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* getObjects()
* Get objects from the teapot model
*/
public void getObjects() {
try {
// Find a specific Camera node in the World.
camera = (Camera) myWorld.find(CAMERA_ID);
myWorld.setActiveCamera(camera);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Make sure that the content is rendered with the correct aspect ratio.
*/
void setupAspectRatio() {
viewport_x = 0;
viewport_y = 0;
viewport_width = myCanvas.getWidth();
viewport_height = myCanvas.getHeight();
Camera cam = myWorld.getActiveCamera();
float[] params = new float[4];
int type = cam.getProjection(params);
if (type != Camera.GENERIC) {
// Calculate the window aspect ratio
float aspectratio = viewport_width / viewport_height;
if (aspectratio < params[1]) {
float height = viewport_width / params[1];
viewport_height = (int) height;
viewport_y = (myCanvas.getHeight() - viewport_height) / 2;
} else {
float width = viewport_height * params[1];
viewport_width = (int) width;
viewport_x = (myCanvas.getWidth() - viewport_width) / 2;
}
}
}
/**
* pauseApp()
*/
public void pauseApp() {
}
public void destroyApp(boolean unconditional)
throws MIDletStateChangeException {
myRefreshTimer.cancel();
myRefreshTimer = null;
myRefreshTask = null;
}
/**
* MIDlet paint method.
*/
public void paint(Graphics g) {
if (g.getClipWidth() != viewport_width
|| g.getClipHeight() != viewport_height
|| g.getClipX() != viewport_x || g.getClipY() != viewport_y) {
g.setColor(0xFF);
g.fillRect(0, 0, myCanvas.getWidth(), myCanvas.getHeight());
}
if ((myGraphics3D != null) && (myWorld != null)) {
//System.out.println("HERE");
myGraphics3D.bindTarget(g);
myGraphics3D.setViewport(viewport_x, viewport_y, viewport_width,
viewport_height);
myGraphics3D.render(myWorld);
myGraphics3D.releaseTarget();
}
}
/**
* Handle commands.
*/
public void commandAction(Command cmd, Displayable disp) {
if (cmd == exitCommand) {
try {
destroyApp(false);
notifyDestroyed();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Inner class for refreshing the view.
*/
private class RefreshTask extends TimerTask {
public void run() {
if (myCanvas != null && myGraphics3D != null && myWorld != null) {
int startTime = (int) System.currentTimeMillis();
int validity = myWorld.animate(startTime);
myCanvas.repaint(viewport_x, viewport_y, viewport_width,
viewport_height);
}
}
}
/**
* Inner class for handling the canvas.
*/
class BoxCanvas extends Canvas {
BoxMidlet myMIDlet;
/**
* Construct a new canvas
*/
BoxCanvas(BoxMidlet midlet) {
myMIDlet = midlet;
}
/**
* Initialize self.
*/
void init() {
}
/**
* Cleanup and destroy.
*/
void destroy() {
}
/*
* Ask myMIDlet to paint itself
*/
protected void paint(Graphics g) {
myMIDlet.paint(g);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -