见如下代码:
定义MathTool.MULTIPLE = 8;
Parabola.java
public class Parabola {
private static Parabola instance = new Parabola();
private static int initX = Const.SCREEN_WIDTH>>1;//0;
private static int initY = Const.SCREEN_HEIGHT>>1;
private static final int ANGLE = 30;
private static final int VELOCITY = 12;
private static final int ACCLY = 2<<MathTool.MULTIPLE;//Y方向上加速度
//circle
private int cycleW = 10;
private int cycleH = 10;
private int time = 0;
private int vx;
private int vy0;
private int vy;
private int px;
private int py;
private int angle;
private int v0;
private Parabola(){
init(ANGLE,VELOCITY);
}
public static Parabola getInstance(){
return instance;
}
public void init(int angles,int v){
angle = angles;
v0 = v;
time = 0;
vx = v0*MathTool.getInstance().Cos(angle);
vy0 = v0*MathTool.getInstance().Sin(angle);
Const.println("vx="+vx);
Const.println("vy="+vy);
vy = -vy0+ACCLY*time/2;
px = (vx*time)>>MathTool.MULTIPLE;
py = (vy*time)>>MathTool.MULTIPLE;
}
public void updateData(){
vy = -vy0+ACCLY*time/2;
px = (vx*time)>>MathTool.MULTIPLE;
py = (vy*time)>>MathTool.MULTIPLE;
time++;
}
public void drawParabolaTrace(Graphics g){
g.setColor(0);
g.fillRect(0, 0, Const.SCREEN_WIDTH, Const.SCREEN_HEIGHT);
g.setColor(0xFF0000);
updateData();
int dx = initX+px;
int dy = initY+py;
g.fillArc(dx, dy, cycleW, cycleH, 0, 360);
if(dx<0||dx>=Const.SCREEN_WIDTH||dy>=Const.SCREEN_HEIGHT||dy<0)
init(ANGLE,VELOCITY);
}
}