gpt4 book ai didi

java - 使用java中的movePolar方法以圆周运动移动GOval对象

转载 作者:行者123 更新时间:2023-12-01 21:57:59 25 4
gpt4 key购买 nike

我目前正在使用 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);
}

}

最佳答案

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;
int i = 0;

private GOval ball;

public void run() {
ball = new GOval (370, 540, BALL_SIZE, BALL_SIZE);
Timer t = new Timer(DELAY_MS, this);
t.start();
}

@Override
public void actionPerformed(ActionEvent e) {
Color color = new Color(0, 0, 255);

add(ball);

i+=30;

ball.setColor(color);
ball.movePolar(130, i);

}

public void init() {
setSize(PROGRAM_WIDTH, PROGRAM_HEIGHT);
}

}

关于java - 使用java中的movePolar方法以圆周运动移动GOval对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58726473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com