gpt4 book ai didi

java - 在圆形路径中移动圆

转载 作者:行者123 更新时间:2023-12-01 10:53:15 26 4
gpt4 key购买 nike

我正在 JPanel 中绘制一个圆圈,并使用 Swing Timer 更新该圆圈的 x,y 坐标。

如何在圆形路径中移动圆。

protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.RED);
Shape planet = new Ellipse2D.Double(x, y, 20, 20);
g2d.fill(planet);
g2d.dispose();
}



public void actionPerformed(ActionEvent evt) {
double R = 200;
for (double t = 0; t < 2 * Math.PI; t += 0.01) {
x = R * Math.cos(t) + 0;
y = R * Math.sin(t) + 0;
revalidate();
repaint();
}
}

最佳答案

你确实已经拥有它了。

x = R * Math.cos(t) + 0;
y = R * Math.sin(t) + 0;

那里的 0 代表圆的中心(分别为 x,y)。因此,要围绕另一个以 0,0 为中心、以 R2 为半径的圆轨道的路径旋转该圆。轨道 theta 值 (oTheta) 每帧都会增加一次。

double R = 200;
double R2 = 1000;
oTheta += 0.1; // depending on your framerate, the more you add, the faster it will orbit
for (double t = 0; t < 2 * Math.PI; t += 0.01) {
x = R * Math.cos(t) + 0 + R2 * Math.cos(oTheta);
y = R * Math.sin(t) + 0 + R2 * Math.sin(oTheta);
revalidate();
repaint();
}

关于java - 在圆形路径中移动圆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33734631/

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