如何在java gui中创建动画矩形?

时间:2023-02-09 23:39:56

To be clear, I want to have a blue circle bouncing left to right which stops on click. This part works. But I also need to have a red rectangle bouncing up and down in the same gui which also stops on click. Each object should stop individually. However, two circles appear where one is moving and the other one is just still. Thanks in Advance! Help is appreciated.

要清楚,我想要一个从左到右弹跳的蓝色圆圈,点击停止。这部分有效。但是我还需要一个红色的矩形在同一个gui中上下弹跳,这个gui也会在点击时停止。每个对象应单独停止。但是,两个圆圈出现在一个正在移动而另一个正在静止的位置。提前致谢!感谢帮助。

/*
 * This program creates an animation for a circle.
 */

package circleanimation;

import java.awt.*;

/*
 * @author talhaiqbal18
 */

public class CircleAnimation
{
    private int centerX, centerY, radius;
    private Color color;
    private int direction, speed;
    private boolean filled;

    public CircleAnimation(int x, int y, int r, Color c) {
        centerX = x;
        centerY = y;
        radius = r;
        color = c;
        direction = 0;
        speed = 0;
        filled = false;
    }

    public void draw(Graphics g) {
        Color oldColor = g.getColor();
        g.setColor(color);
        if (filled) {
            g.fillOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
            g.fillRect(200, 200, 200, 200); }
        else {
            g.fillRect(200, 200, 200, 200);
            g.fillOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
            g.setColor(oldColor); }
    }

    public void fill(Graphics g) {
        Color oldColor = g.getColor();
        g.setColor(color);
        g.fillOval(centerX - radius, centerY - radius, radius * 2, radius * 2);
        g.fillRect(200, 200, 200, 200);
        g.setColor(oldColor);
    }

    public boolean containsPoint(int x, int y) {
       int xSquared = (x - centerX) * (x - centerX);
       int ySquared = (y - centerY) * (y - centerY);
       int radiusSquared = radius * radius;
       return xSquared + ySquared - radiusSquared <= 0;
    }

    public void move(int xAmount, int yAmount) {
        centerX = centerX + xAmount;
        centerY = centerY + yAmount;
    }

    public int getRadius() {
        return radius;
    }

    public int getX() {
        return centerX;
    }

    public int getY() {
        return centerY;
    }

    public void setSpeed(int s) {
        speed = s;
    }

    public void setDirection(int d) {
        direction = d % 360;
    }

    public void turn(int degrees) {
        direction = (direction + degrees) % 360;
    }

    public void move() {
        move((int)(speed * Math.cos(Math.toRadians(direction))),
            (int)(speed * Math.sin(Math.toRadians(direction))));
    }

    public void setFilled(boolean b) {
        filled = b;
    }
}
/*
 * This is the color panel class for the circle animation project.
 */

package circleanimation;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/*
 * @author talhaiqbal18
 */

public class ColorPanel extends JPanel
{
    private CircleAnimation circle, rectangle;
    private javax.swing.Timer timer;
    private CircleAnimation selectedCircle, selectedRectangle;
    private int x, y;

    public ColorPanel(Color backColor, int width, int height) {
        setBackground(backColor);
        setPreferredSize(new Dimension(width, height));
        circle = new CircleAnimation(350, 300, 350, Color.blue);
        circle.setDirection(180);
        circle.setSpeed(6);
        rectangle = new CircleAnimation(350, 300, 400, Color.red);
        rectangle.setDirection(90);
        rectangle.setSpeed(6);
        timer = new javax.swing.Timer(1, new MoveListener());
        timer.start();
        addMouseListener(new PanelListener());
        addMouseMotionListener(new PanelMotionListener());
        addMouseMotionListener(new PanelMotionListener1());
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        circle.fill(g);
        rectangle.fill(g);
    }

    private class MoveListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            int x = circle.getX();
            int radius = circle.getRadius();
            int width = getWidth();
            if (x - radius <= 0 || x + radius >= width) {
                circle.turn(180);
            }
            circle.move();
            repaint();
        }
    }

    private class MoveListener1 implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            int x = rectangle.getX();
            int radius = rectangle.getRadius();
            int width = getWidth();
            if (x - radius <= 0 || x + radius >= width) {
                rectangle.turn(270);
            }
            rectangle.move();
            repaint();
        }
    }

    private class PanelListener extends MouseAdapter {
        public void mousePressed(MouseEvent e)
        {
            x = e.getX();
            y = e.getY();
            if (circle.containsPoint(x, y))
                selectedCircle = circle;
            if (rectangle.containsPoint(x, y))
                selectedRectangle = rectangle;
        }

        public void mouseReleased(MouseEvent e) {
            //nothing
        }

        public void mouseClicked(MouseEvent e) {
            if(timer.isRunning())
                timer.stop();
            else
                timer.start();
        }
    }

    private class PanelMotionListener extends MouseMotionAdapter
    {
        public void mouseDragged(MouseEvent e)
        {
                int newX = e.getX();
                int newY = e.getY();
                int dx = newX - x;
                int dy = newY - y;
            if (selectedCircle != null) {
                selectedCircle.move(dx,dy);
                x = newX;
                y = newY;
                repaint(); }
        }
    }

    private class PanelMotionListener1 extends MouseMotionAdapter
    {
        public void mouseDragged(MouseEvent e)
        {
                int newX = e.getX();
                int newY = e.getY();
                int dx = newX - x;
                int dy = newY - y;
            if (selectedRectangle != null) {
                selectedRectangle.move(dx,dy);
                x = newX;
                y = newY;
                repaint(); }
        }
    }
}
/*
 * This is the main method which will implement the actions of the previous classes.
 */

package circleanimation;

import java.awt.*;
import javax.swing.JFrame;

/*
 * @author talhaiqbal18
 */

public class MainClass
{
    public static void main(String[] args) throws Exception {
        JFrame theGUI  = new JFrame();
        theGUI.setTitle("Circle Animation");
        theGUI.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        ColorPanel panel = new ColorPanel(Color.white, 100, 100);
        Container pane = theGUI.getContentPane();
        pane.add(panel);
        theGUI.setVisible(true);
    }
}

1 个解决方案

#1


You only register a timer for your circle, but not for your rectangle in the ColorPanel constructor

您只为圆圈注册计时器,但不为ColorPanel构造函数中的矩形注册

#1


You only register a timer for your circle, but not for your rectangle in the ColorPanel constructor

您只为圆圈注册计时器,但不为ColorPanel构造函数中的矩形注册