Java Swing GUI扭曲动画

k5ifujac  于 2023-01-19  发布在  Java
关注(0)|答案(1)|浏览(145)

我想建立一个三个球朝一个方向运动的简单动画。因此我创建了一个计时器,它在每一个滴答声中编辑球的坐标,但是当我试着运行它的时候球就扭曲了。它们正以正确的速度和方向移动,但是当它们改变坐标的时候我想移除旧的球。我试着在paint方法中添加super.paint()。它It“我们正在删除旧球,但大多数时候帧只是白色的。

public class Animation extends JFrame{
    private JPanel panel2;
    int maus1x = 110;
    int maus1y = 350;
    int maus2x = 55;
    int maus2y = 350;
    int maus3x = 0;
    int maus3y = 350;
    ActionListener timeListener = new TimeListener();
    Timer timer = new Timer(100,timeListener);

    public Animation() {
        this.setContentPane(this.panel2);
        this.setTitle("MausKampf");
        this.setSize(500, 500);
        this.setResizable(false);
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        timer.start();
    }

    public void paint(Graphics g) {
        Graphics2D g2D = (Graphics2D) g;
        g2D.setColor(Color.black);
        g2D.drawLine(0,400,500,400);
        g2D.drawLine(0,350,225,350);
        g2D.drawLine(275,350,500,350);
        g2D.drawLine(225,350,225,0);
        g2D.drawLine(275,350,275,0);
        g2D.setPaint(Color.CYAN);
        g2D.fillOval(maus1x,maus1y,50,50);
        g2D.setPaint(Color.GREEN);
        g2D.fillOval(maus2x,maus2y,50,50);
        g2D.setPaint(Color.RED);
        g2D.fillOval(maus3x,maus3y,50,50);
    }

    public void move() {
        maus1x = maus1x + 4;
        maus2x = maus2x + 4;
        maus3x = maus3x + 4;
        if (maus1x > 325) {
            timer.stop();
        }
        repaint();
    }

    public class TimeListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            move();
        }
    }
}
hpcdzsge

hpcdzsge1#

  • Swing* 应用程序不必扩展JFrame。但是,如果要执行custom painting,则应编写一个扩展JPanel的类并覆盖其paintComponent方法,覆盖方法中的第一行应为:super.paintComponent(g);

我对你问题中的鳕鱼做了一些小改动。

  • 首先,类Animation扩展了JPanel,而不是JFrame
  • 其次,我将方法paint重命名为paintComponent,并添加了上面描述的行。
  • 最后,我添加了方法main,使其成为runnable
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.WindowConstants;

public class Animation extends JPanel {
    int maus1x = 110;
    int maus1y = 350;
    int maus2x = 55;
    int maus2y = 350;
    int maus3x = 0;
    int maus3y = 350;
    ActionListener timeListener = new TimeListener();
    Timer timer = new Timer(100,timeListener);

    public Animation() {
        JFrame frame = new JFrame("MausKampf");
        frame.setContentPane(this);
        frame.setSize(500, 500);
        frame.setResizable(false);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
        timer.start();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2D = (Graphics2D) g;
        g2D.setColor(Color.black);
        g2D.drawLine(0,400,500,400);
        g2D.drawLine(0,350,225,350);
        g2D.drawLine(275,350,500,350);
        g2D.drawLine(225,350,225,0);
        g2D.drawLine(275,350,275,0);
        g2D.setPaint(Color.CYAN);
        g2D.fillOval(maus1x,maus1y,50,50);
        g2D.setPaint(Color.GREEN);
        g2D.fillOval(maus2x,maus2y,50,50);
        g2D.setPaint(Color.RED);
        g2D.fillOval(maus3x,maus3y,50,50);
    }

    public void move() {
        maus1x = maus1x + 4;
        maus2x = maus2x + 4;
        maus3x = maus3x + 4;
        if (maus1x > 325) {
            timer.stop();
        }
        repaint();
    }

    public class TimeListener implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent e) {
            move();
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new Animation());
    }
}

相关问题