-4

I wrote a program that makes rectangles move. But so far they move only up and down. Now I need to make them move in circles(endless). Here is what I have already done:

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);
        }
    }
}
HansUp
  • 95,961
  • 11
  • 77
  • 135
user2876296
  • 97
  • 2
  • 7

2 Answers2

2

In this code you are only changing the y coordinate.

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

You can similarly change the x coordinate as well according to the circle's equation.

For example if the centre of circle is (x0,y0) and radius is r, you can set the initial value of theta, make small changes in theta and update the new x and y coordinates of the rectangle.

x = x0 + r*cos(theta_initial + delta_theta)
y = y0 + r*sin(theta_initial + delta_theta)
Abhishek Bansal
  • 12,589
  • 4
  • 31
  • 46
0

This code is taken from TheChernoProject 3D Game Programming 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);

All you need to do is place this inside your paintComponent or whatever you use to render your rectangles and then use the x & y values.

Linus
  • 1,516
  • 17
  • 35