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

标签 java user-interface animation

要明确的是,我想要一个从左到右弹跳的蓝色圆圈,单击时停止。这部分有效。但我还需要有一个红色矩形在同一个图形用户界面中上下弹跳,它也会在单击时停止。每个物体应该单独停止。然而,会出现两个圆圈,一个在移动,另一个则静止。提前致谢!感谢帮助。

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

最佳答案

您仅在 ColorPanel 构造函数中为圆形注册计时器,而不为矩形注册计时器

关于java - 如何在java gui中创建动画矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30088400/

相关文章:

java - 如何从给定的两个字符串数组形成第三个java数组

java - 如何为自定义处理器配置 "initialise"方法

ios - 使用开放动画(展开)快速制作轮播 View 的更好方法是什么?

android - 用户从下拉列表中选择项目后禁用 Android AutoCompleteTextView

java - 使用 Eclipse (Java) 创建 GUI

html - CSS 3 - 缩放过渡在谷歌浏览器中快速返回

javascript - 如何在 D3.js 中为笔画的粗细设置动画?

java - MySQL 排序规则和 Glassfish 默认字符集编码

java - Tomcat 7 "SEVERE: A child container failed during start"

c# - 暂停/恢复 WPF Storyboard