我当前正在使用ACM图形库创建一个小项目,在该项目中,您将必须使用方法GOval移动movePolar的球(或对象)以作圆周运动。到目前为止,我知道movePolar()
中有两个参数,其中第一个r
参数是您想要移动的距离,第二个参数是您想要移动的角度。但是我做不到找出如何使用movePolar()
方法使球以圆周运动运动。我尝试使用多个movePolar(1,90)
,movePolar(1,45)
等,但仍然达不到我想要的目标。这是我的代码:
import java.awt.Color;
import java.awt.event.actionEvent;
import java.awt.event.actionListener;
import javax.swing.Timer;
import acm.graphics.*;
import acm.program.*;
public class ColorCircleDecomp extends GraphicsProgram implements actionListener {
public static final int PROGRAM_WIDTH = 800;
public static final int PROGRAM_HEIGHT = 600;
public static final int BALL_SIZE = 50;
public static final int DELAY_MS = 25;
private GOval ball;
//TODO create a private GOval here
public void run() {
//TODO add your ball here
Timer t = new Timer(DELAY_MS,this);
ball = new GOval(300,300,BALL_SIZE,BALL_SIZE);
add(ball);
t.start();
}
@Override
public void actionPerformed(actionEvent e) {
ball.movePolar(1,90);
ball.movePolar(1,0);
ball.movePolar(1,270);
ball.movePolar(1,180);
}
public void init() {
setSize(PROGRAM_WIDTH,PROGRAM_HEIGHT);
}
}