J2ME 3D Programming--The first 3D program

Source: Internet
Author: User
Tags thread
Programming | program J2ME 3D Programming--The first 3D program (with source code)

More articles please visit: Http://blog.csdn.net/mailbomb



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 the Redraw interface thread

Thread thread = new thread (this);

Thread.Start ();

try {

Import 3D Pictures

MyWorld = (World) loader.load ("/swerve.m3g") [0];

viewport_x = 0;

viewport_y = 0;

Viewport_width = GetWidth ();

Viewport_height = GetHeight ();

Cam = Myworld.getactivecamera ();

Setting up a 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) {

Redrawing a graphic

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.