Header Sep
Java Tips, Tricks & Code
 
My Rating Score
Login to rate page

November 2004
Rotating cube in Micro3D and JSR-184 using the same code

[Back]

 

We will show how to make two simple 3D MIDlets, one that uses the JSR 184 API and the other that uses the Micro3D version 3 API. The MIDlets demonstrate how to use both of these APIs to produce the same 3D animation. Because the example code uses immediate mode operations, no 3D content files are required.

  • 3D_Demo1 – contains two source files, "MIDletMain.java" and "MyCanvas.java". The example code uses the JSR 184 API. The subdirectory res stores the image files cubeface.png and backdrop.png that serve as a texture map and a background image, respectively.
  • 3D_Demo2 – contains two source files, "SampleApp.java" and "SampleCanvas.java". The example code uses the Micro3D version 3 API. The subdirectory res stores the image files cubeface.bmp and backdrop.png that serve as a texture map and a background image. Note that Micro3D version 3 uses 8-bit BMP format for its texture map image.

The code in the "MIDletMain.java" and "SampleApp.java" files is identical; the file names were changed to prevent confusion.

These files perform the standard MIDlet initialization and start a timer thread that implements the scene's animation. The initialization code next creates an instance of a custom Canvas. Its paint method makes 3D API calls that generate a cube and apply the image in "cubeface.png" or "cubeface.bmp" as a texture map onto its faces. The method also performs a matrix transformation that changes the cube's orientation slightly.

The timer thread periodically calls the Canvas's repaint() method, which continues to change the cube's orientation and thus generates the rotation effect.

These simple demos show that if the MIDlet is carefully designed, it is possible to substitute the code that makes JSR 184 API calls with the code that makes Micro3D version 3 calls without modifying the MIDlet's core logic.

Download an example with source code>>

 

My Rating Score
Login to rate page