Java Reference
In-Depth Information
Consider the TriangleMIDlet , shown in Figure 15-2. You can try it out from the source code
download. This MIDlet simply rotates a triangle, which will appear red on your screen, in 2D
when you start it. Watch it for a while.
Figure 15-2. TriangleMIDlet rotating a 2D triangle
You should be able to imagine how you'd create this animation program without using a
single line of M3G code:
Create a subclass of Canvas .
1.
In the paint() method of the Canvas (or the animation loop for a GameCanvas ), you will
draw the triangle, and then set a timer to repaint. In each successive repaint (frame),
you will draw the triangle in a new position. The coordinates can be calculated using
simple trigonometry.
2.
Create a MIDlet to host the custom Canvas .
3.
Figure 15-3 shows how you can animate the triangle in this MIDlet.
In Figure 15-3, initially the three vertices (0,0) , (3,0) , and (3,3) define the triangle. Subse-
quent frames of the animation rotate the triangle around the origin at (0,0) .
Now that you are satisfied with your ability to create the same effect using MIDP's 2D graphics
capability, we'll reveal to you that this animation is rendered using M3G. Yes, the M3G 3D API
is used to rotate the triangle around the origin!
The geometry engine built into M3G understands the math of the rotation and recalculates the
coordinates, plus redraws the triangle in each frame for you. All that you need to do is tell it the
initial coordinates of the triangle and around what to rotate that triangle.
At this point, you are probably wondering what all of this has to do with 3D graphics. Here
is the last secret that we have been keeping from you: the rotation you see in TriangleMIDlet is
actually in 3D!
 
Search WWH ::




Custom Search