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;
} } }新聞熱點
疑難解答