Java 需要帮助写一个矩形在圆中移动的算法吗

Java 需要帮助写一个矩形在圆中移动的算法吗,java,algorithm,swing,Java,Algorithm,Swing,我写了一个程序,使矩形移动。但到目前为止,它们只是上下移动。现在,我需要让他们在圆圈里移动(无止境)。以下是我已经做过的: import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java

我写了一个程序,使矩形移动。但到目前为止,它们只是上下移动。现在,我需要让他们在圆圈里移动(无止境)。以下是我已经做过的:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

class GrimMain {
    static JFrame frame = new JFrame();
    //final static List<Rect> rectangles = new ArrayList<Rect>();
    //final static int count = 0;

    public static void main(String[] args) {
        DrawingComponent fps = new DrawingComponent();
        int pos = 100;
        Rect r1 = new Rect(pos+100, 100, 30, 30, Color.red);
        Rect r2 = new Rect(pos+140, 100, 30, 30, Color.blue);
        Rect r3 = new Rect(pos+180, 100, 30, 30, Color.green);

        fps.addRect(r1);
        fps.addRect(r2);
        fps.addRect(r3);
        fps.animate();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fps.setPreferredSize(new Dimension(800, 600));
        frame.getContentPane().add(fps);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class Rect {
    int x;
    int y;
    int height;
    int width;
    Color color;
    Rect(int x, int y, int width, int height, Color color) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.color = color;
    }

    public void draw(Graphics g) {
        g.setColor(color);
        g.fillRect(x, y, width, height);
    }

    public int getY() {
        return y;
    }
    public int getX() {
        return x;
    }

    public int getHeight() {
        return height;
    }

    public void setY(int y) {
        this.y = y;
    }
    public void setX(int y) {
        this.x = x;
    }
}

class DrawingComponent extends JComponent {
    private static final int ANIMATION_DELAY = 10;
    private List<Rect> rectList = new ArrayList<Rect>();
    private int deltaY = 2;

    DrawingComponent() {
    }

    public void animate() {
// here is the part with animation
        new Timer(ANIMATION_DELAY, new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                for (Rect rect : rectList) {
                    int y = rect.getY();
                    if (y + rect.getHeight() >= getHeight()) {
                        deltaY = -Math.abs(deltaY);
                    }
                    else if (y <= 0) {
                        deltaY = Math.abs(deltaY);
                    }

                    rect.setY(y + deltaY);
                }
                repaint();
            }
        }).start();
    }

    public void addRect(Rect rect) {
        rectList.add(rect);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Rect rect : rectList) {
            rect.draw(g);
        }
    }
}
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.Graphics;
导入java.awt.event.ActionEvent;
导入java.awt.event.ActionListener;
导入java.util.ArrayList;
导入java.util.List;
导入javax.swing.*;
格里曼类{
静态JFrame=新JFrame();
//最终静态列表矩形=新的ArrayList();
//最终静态整数计数=0;
公共静态void main(字符串[]args){
DrawingComponent fps=新DrawingComponent();
int pos=100;
Rect r1=新的Rect(位置+100,100,30,30,颜色为红色);
Rect r2=新的Rect(位置+140、100、30、30,颜色为蓝色);
Rect r3=新的Rect(位置+180、100、30、30,颜色为绿色);
fps.addRect(r1);
fps.addRect(r2);
fps.addRect(r3);
fps.animate();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fps.setPreferredSize(新尺寸(800600));
frame.getContentPane().add(fps);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
}
类矩形{
int x;
int-y;
内部高度;
整数宽度;
颜色;
矩形(整数x,整数y,整数宽度,整数高度,颜色){
这个.x=x;
这个。y=y;
这个。宽度=宽度;
高度=高度;
这个颜色=颜色;
}
公共空间绘制(图g){
g、 设置颜色(颜色);
g、 fillRect(x,y,宽度,高度);
}
公共int getY(){
返回y;
}
公共int getX(){
返回x;
}
公共整数getHeight(){
返回高度;
}
公共空间设置(整数y){
这个。y=y;
}
公共无效集合x(整数y){
这个.x=x;
}
}
类DrawingComponent扩展了JComponent{
私有静态最终int动画_延迟=10;
private List rectList=new ArrayList();
私人int deltaY=2;
DrawingComponent(){
}
public void animate(){
//下面是动画部分
新计时器(动画延迟,新ActionListener(){
已执行的公共无效操作(操作事件arg0){
for(Rect-Rect:rectList){
int y=rect.getY();
如果(y+rect.getHeight()>=getHeight()){
deltaY=-Math.abs(deltaY);
}

否则,如果(y在该代码中,您仅更改y坐标

for (Rect rect : rectList) {
                    int y = rect.getY();
                    if (y + rect.getHeight() >= getHeight()) {
                        deltaY = -Math.abs(deltaY);
                    }
                    else if (y <= 0) {
                        deltaY = Math.abs(deltaY);
                    }

                    rect.setY(y + deltaY);
                }

此代码取自Chernoproject 3D游戏编程


您所需要做的就是将其放置在您的paintComponent或用于渲染矩形的任何东西中,然后使用x&y值。

将检查一个相关示例。
x = x0 + r*cos(theta_initial + delta_theta)
y = y0 + r*sin(theta_initial + delta_theta)
int x = (int) (Math.sin(System.currentTimeMillis() % 2000.0 / 2000 * Math.PI * 2) * 200);
int y = (int) (Math.cos(System.currentTimeMillis() % 2000.0 / 2000 * Math.PI * 2) * 200);