As we all know, CLDC1.0 does not support decimal operations, while CLDC1.1 only supports floating point operations. However, most mobile phones on the market currently use the CLDC1.0 configuration. What should we do with the decimal operation? For example, you need to draw an airplane running track from any angle.
Of course you can write a class by yourself and use Integers to simulate fixed-point decimal operations (it is very difficult to simulate floating-point decimal operations), but you do not have to re-invent the wheel, there are many code libraries on the Internet that use integer operations to simulate decimal operations. MathFP is one of the most outstanding code libraries. It is robust, stable, and fast, it is the best choice for performing decimal operations in the j2's environment, and the most important thing is that it is very small in size. MathFP is: http://home.rochester.rr.com/ohom
Mes/MathFP.
The version I downloaded is based on CLDC, And the MathFP version is 1.1.2. all the downloaded content is a MathFP. class (the package name of this class is net. jscience. util), you can place the class in your classpath for development, and add the class to the jar file when releasing the software.
Alternatively, you can decompile the class to obtain the source code and directly put it into the src directory of your project. Here I use the latter method. Remember to download the API documentation of MathFP at the same time. Because MathFP uses Integers to simulate decimal points, the internal representation of decimal points is an integer, but you must remember to distinguish the integer representing the decimal points from the real integer, otherwise, it will cause many bugs that are difficult to debug (a trick is to indicate that the integer variable name of the decimal number is suffixed with FP ). You only need to master one principle: first, convert all integers involved in the decimal operation to decimal places, and then perform the decimal operation. After the calculation, convert the result to an integer.
The following example demonstrates the basic usage of MathFP. Assume that there is a point in the lower left corner of the screen. Every 100 milliseconds, five pixels are moved to the northeast along the 60 degrees angle to draw the trajectory of this point. This example involves decimal and triangular operations, because the increment of the x coordinate of this point is cos60 (degree), and the increment of the y coordinate is-sin60 (degree ). Let's take a look at how to write the code:
Import javax. microedition. lcdui. Canvas;
Import javax. microedition. lcdui. Graphics;
Import net. jscience. util. MathFP;/*** demonstration Canvas * @ author Jagie **/
Public class FloatCanvas extends Canvas implements Runnable
{// Used to count the number of screen refreshes
Int paintCount;
// Screen width and height. Number of points
Int w_FP, h_FP; // the coordinate of the current vertex, the coordinate of the previous vertex, and the number of points.
Int curX_FP, curY_FP,
LastX_FP, lastY_FP; // Rate
Public static final int RATE = 5;
Public FloatCanvas (){
W_FP = MathFP. toFP (this. getWidth ());
H_FP = MathFP. toFP (this. getHeight ());
// The start point is in the lower left corner of the screen.
LastX_FP = MathFP. toFP (0 );
LastY_FP = h_FP;
New Thread (this). start ();
}
Protected void paint (Graphics g)
{// Only clear the screen for the first time
If (paintCount = 0)
{
G. setColor (0 );
G. fillRect (0, 0, w_FP, h_FP );
} Else
{
// Draw a line
G. setColor (0x00ff00 );
// Convert the number of points to an integer
G. drawLine (MathFP. toInt (lastX_FP ),
MathFP. toInt (lastY_FP), MathFP
. ToInt (curX_FP), MathFP. toInt (curY_FP ));
}
PaintCount ++;
}
Public void run ()
{
// Loop when the current vertex does not exceed the screen
While (curX_FP <= w_FP &&
CurY_FP <= h_FP
& MathFP. toInt (curX_FP)> = 0
& MathFP. toInt (curY_FP)> = 0 ){
// Converts 60 degrees to radians
Int radians =
MathFP. div (MathFP. mul (MathFP. toFP (60 ),
MathFP. PI ),
MathFP. toFP (180 ));
// Increment in the x direction
Int deltaX = MathFP. mul (MathFP. toFP (RATE ),
MathFP. cos (radians ));
// Increment in the y direction
Int deltaY = MathFP. mul (MathFP. toFP (RATE ),
MathFP. sin (radians); // new coordinate, number of points
CurX_FP = lastX_FP + deltaX;
CurY_FP = lastY_FP-deltaY;
System. out. println (curX_FP + "," + curY_FP );
Repaint ();
Try {
Thread. sleep (100 );
} Catch (InterruptedException e ){
// TODO Auto-generated catch block
E. printStackTrace ();}
// The new coordinates are the old coordinates.
LastX_FP = curX_FP;
LastY_FP = curY_FP;
}}}
The Canvas is drawn on the device as follows:
As you can see, the curve is continuously increasing along the direction of the 60 degrees angle to the east and north. With this fixed-point database, we can use decimal operations in the game, so some simple game physical algorithms can also be used.