J2ME 3D Programming--The first 3D program (with source code)

Source: Internet
Author: User
Tags thread

Reference WTK2.2 provided the demo, completed the first 3D program, although very simple, and some problems are not very clear, or to share the code and is willing to learn J2ME 3D programming friends to study together.

About 3D programming information, you can check my blog.

A description of the compilation and operation of the code is as follows:

1. The following code is compiled and passed under J2ME WTK2.2.

2, the code is divided into two files: First3dcanvas.java and First3dmidlet.java.

3, the use of J2ME WTK2.2 to establish a new project, the main MIDlet class is: First3d. First3dmidlet

4, save the code in your engineering directory under the First3d directory.

5, will j2me WTK installation directory under the

The swerve.m3g files in the Apps\demo3d\res\com\superscape\m3g\wtksamples\retainedmode\content directory are copied to the Res directory under your engineering directory.

6. After your project is established, set up the project, through the WTK interface of the "Settings" button to open the Settings window, in "API Selection", set the "target platform" is: custom; "Profile" is "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 redrawing the interface thread


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 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 < params[1]) {


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 ());


//And 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) {}


}


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.