J2ME 3D programming - the first 3D program (attachment code)
More articles please visit: http://blog.9cbs.net/mailbomb
Refer to Demo provided by WTK2.2, complete the first 3D program, although it is very simple, and some problems are not very clear, they still share the code together and is willing to learn J2ME 3D programming.
About 3D programming information, you can check my blog.
The compilation and operation description of the code is as follows:
1. The following code is compiled under J2ME WTK2.2.
2, the code is divided into two files: first3dcanvas.java and first3dmidlet.java.
3. Use J2ME WTK2.2 to establish new projects, the main MIDET class is: first3d. First3dmidlet
4. Save the code in the first3d directory under your project directory.
5, put the J2ME WTK installation directory
The Apps / Demo3D / RES / COM / SUPERSCAPE / M3G / WTKSAMPLES / RETAINEDMODE / Content Directory is copied to the rescription under your project directory.
6. After your project is established, set the project, open the settings window through the "Settings" button in the WTK interface, set "Target Platform" in "API Selection"; "Contributor" "MIDP2.0 ";" Configuration "is" CLDC1.1 "; select" Mobile 3D Graphics For J2ME (JSR184) ".
7, so you can compile and run the following code.
The source code is as follows:
// first3dmidlet.java
Package first3d;
Import javax.microedition.midlet. *;
Import javax.microedition.lcdui. *;
Public class first3dmidlet extends MIDlet {
Private first3dcanvas displayable = new first3dcanvas ();
Public void startapp () {
Display.getdisplay (this) .SetCurrent (Displayable);
}
Public void pauseapp () {}
Public void destroyApp (boolean unconditional) {}
}
// first3dcanvas.java
Package first3d;
Import javax.microedition.lcdui. *;
Import javax.microedition.m3g. *;
Import java.util. *;
/ **
* First 3D program
* /
Public Class First3Dcanvas
Extends Canvas
Implements runnable {
/ ** World object * /
Private world myworld = null;
/ ** graphics3D object * /
Private graphics3d g3d = graphics3d.getinstance ();
/ ** CAMERA object * /
Private Camera Cam = NULL;
Private Int ViewPort_x;
Private int viewport_y;
PRIVATE INT ViewPort_Width;
Private Int ViewPort_Height;
Private long worldstarttime = 0;
// Redraw time
PRIVATE INT VALIDITY = 0;
Public first3dcanvas () {// Start the thread of the redraw interface
Thread thread = new thread (this);
Thread.start ();
Try {
// Import 3D picture
MyWorld = (world) loader.load ("/ swerve.m3g") [0];
ViewPort_x = 0;
ViewPort_y = 0;
Viewport_Width = getWidth ();
ViewPort_height = getHeight ();
Cam = myWorld.getActiveCamera ();
// Set the CAM object
Float [] params = new float [4];
INT TYPE = Cam.getProjection (params);
IF (Type! = Camera.Generic) {
// Calculate Window Aspect Ratio
FLOAT WASPECT = viewport_width / viewport_height;
IF (Waspect Float Height = ViewPort_Width / params [1]; ViewPort_height = (int) height; ViewPort_Y = (GetHeight () - ViewPort_Height) / 2; } Else { Float width = viewport_height * params [1]; ViewPort_Width = (int) width; ViewPort_x = (getWidth () - ViewPort_Width / 2; } } WorldStartTime = system.currenttimemillis (); } Catch (Exception E) {} } Protected Void Paint (Graphics G) { // Clear background g.setcolor (0x00); G.fillRect (0, 0, getWidth (), getHeight ()); / / 3D object binding g3d.bindtarget (g); g3d.setViewPort (viewport_x, viewport_y, viewport_width, viewport_height); Long StartTime = System.currentTimeMillis () - WorldStartTime; Validity = myWorld.Animate ((int) starttime); Try { g3d.render (myWorld); } Finally { g3d.releasetarget (); } } Public void run () { Try { While (true) { // Redraw graphics Repaint (viewport_x, viewport_y, viewport_width, viewport_height); } } catch (exception e) {} } }