在这个颜值当道,屌丝闪边的时代,拼不过颜值拼内涵,只有知识丰富才能提升一个人的内在气质和修养,所谓人丑就要多学习,今天图老师给大家分享J2ME中定点库MathFP使用入门,希望可以对大家能有小小的帮助。
【 tulaoshi.com - 编程语言 】
众所周知,CLDC1.0是不支持小数运算的,而CLDC1.1才支持浮点运算。但是目前市面上的手机,绝大部分是采用CLDC1.0这种configuration。那我们需要进行小数运算怎么办呢?比如说要绘制任意角度的飞机运行轨迹。import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import net.jscience.util.MathFP; /** * 小数运算演示Canvas * @author Jagie * */
public class FloatCanvas extends Canvas implements Runnable
{ //用于统计屏幕刷新次数
int paintCount;
//屏幕宽度,高度。定点数
int w_FP, h_FP; //当前点坐标,前一点坐标,定点数
int curX_FP, curY_FP,
lastX_FP, lastY_FP; //速率
public static final int RATE = 5;
public FloatCanvas() {
w_FP = MathFP.toFP(this.getWidth());
h_FP = MathFP.toFP(this.getHeight());
//开始点处于屏幕的左下角
lastX_FP = MathFP.toFP(0);
lastY_FP = h_FP;
new Thread(this).start();
}
protected void paint(Graphics g)
{ //第一次只是清屏
if (paintCount == 0)
{
g.setColor(0);
g.fillRect(0, 0, w_FP, h_FP);
} else
{
//画线
g.setColor(0x00ff00);
//把定点数转换成整数
g.drawLine(MathFP.toInt(lastX_FP),
MathFP.toInt(lastY_FP), MathFP
.toInt(curX_FP), MathFP.toInt(curY_FP));
}
paintCount++;
}
public void run()
{
//当前点没有超出屏幕时循环
while (curX_FP = w_FP &&
curY_FP = h_FP
&& MathFP.toInt(curX_FP) = 0
&& MathFP.toInt(curY_FP) = 0) {
//60度角度转换成弧度
int radians =
MathFP.div(MathFP.mul(MathFP.toFP(60),
MathFP.PI),
MathFP.toFP(180));
//x方向增量
int deltaX = MathFP.mul(MathFP.toFP(RATE),
MathFP.cos(radians));
//y方向增量
int deltaY = MathFP.mul(MathFP.toFP(RATE),
MathFP.sin(radians)); //新坐标,定点数
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(); }
//新坐标成为旧坐标
lastX_FP = curX_FP;
lastY_FP = curY_FP;
} } }
来源:http://www.tulaoshi.com/n/20160219/1599412.html
看过《J2ME中定点库MathFP使用入门》的人还看了以下文章 更多>>