【问题标题】:Need help to write an algorithm for rectangles to move in circles [closed]需要帮助来编写一个用于矩形移动的算法[关闭]
【发布时间】:2013-11-23 15:00:11
【问题描述】:

我写了一个让矩形移动的程序。但到目前为止,它们只能上下移动。现在我需要让它们转圈(无止境)。这是我已经完成的:

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);
        }
    }
}

【问题讨论】:

  • 检查了一个相关的例子here

标签: java algorithm swing


【解决方案1】:

在此代码中,您只更改 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);
                }

你也可以根据圆的方程同样改变x坐标。

例如,如果圆心为(x0,y0),半径为r,您可以设置theta的初始值,对theta进行微小的更改,并更新矩形的新x和y坐标。

x = x0 + r*cos(theta_initial + delta_theta)
y = y0 + r*sin(theta_initial + delta_theta)

【讨论】:

    【解决方案2】:

    此代码取自 TheChernoProject 3D 游戏编程episode 5

    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);
    

    您需要做的就是将它放在您的paintComponent 或您用来渲染矩形的任何东西中,然后使用x 和y 值。

    【讨论】:

      猜你喜欢
      • 2021-11-12
      • 2018-12-07
      • 2011-03-25
      • 1970-01-01
      • 2012-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      相关资源
      最近更新 更多