Java 单击按钮后显示并启动带有绘制动画的JPanel

Java 单击按钮后显示并启动带有绘制动画的JPanel,java,swing,timer,Java,Swing,Timer,从24小时前开始,我一直在做这件事。我有一个自定义的jpanel,在它上面使用定时器绘制一个简单的动画。我想要的是在Pikachu和Sasuke之间的中间显示这个面板(我一直在尝试,直到现在),这样动画才会开始,并且只有在点击这个按钮时才会发生 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class App extends JFrame { private static JPanel p53

从24小时前开始,我一直在做这件事。我有一个自定义的jpanel,在它上面使用定时器绘制一个简单的动画。我想要的是在Pikachu和Sasuke之间的中间显示这个面板(我一直在尝试,直到现在),这样动画才会开始,并且只有在点击这个按钮时才会发生

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

public class App extends JFrame {
private static JPanel p53 = new JPanel();
private static JButton fire = new JButton("Attack");
private AttackFX attackfx = new AttackFX();

public App() {

fire.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent atk) {
                    p53.add(attackfx);
                    Timer timer1 = new Timer(800, new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent ld2) {
                        }
                    });
                    timer1.start();
                    timer1.setRepeats(false);
            }
        });
}

public static void main(String[] a) {
App Battleframe = new App();
Battleframe.add(fire);
Battleframe.add(p53);
Battleframe.setResizable(false);
Battleframe.setTitle("OnFight.Combat_Arena_Beta");
Battleframe.setSize(381, 400);
Battleframe.setLocationRelativeTo(null);
Battleframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Battleframe.setVisible(true);
}

class AttackFX extends JPanel {

    private int xCoor = 1;

 public AttackFX() {
 Timer tm1 = new Timer(100, new TimerListener2());
 tm1.start();
 tm1.setRepeats(true);
 }

 @Override
 protected void paintComponent(Graphics g) {
 super.paintComponent(g);

 xCoor += 1;

 g.setColor(Color.cyan);
 g.fillOval(xCoor, 40, 8, 8);
 }

 class TimerListener2 implements ActionListener {
 @Override
 public void actionPerformed(ActionEvent e) {
 repaint();
 }
 }

}
}

我真的需要你的帮助。明天是我整个节目的演示。这是实现这一目标的最后一步。我已经尽我所能独自解决这个问题,但没有成功

以下是一些工作代码,单击按钮时会生成一个圆圈,并在点击某个x值后使其消失:

public class App extends JFrame implements ActionListener {

  private static final long serialVersionUID = 1L;

  public static void main(String[] a) {
    App app = new App();
    app.createControls();
  }

  public App() {
    setResizable(false);
    setTitle("OnFight.Combat_Arena_Beta");
    setSize(381, 400);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
  }

  private void createControls() {
    JPanel upper = new JPanel();

    JButton fire = new JButton("Attack");
    fire.addActionListener(this);
    upper.add(fire);

    this.add(upper, BorderLayout.NORTH);
    setVisible(true);
  }

  /*
   * When the "Attack" button is pressed the time will start and the
   * circle will start painting.
   */
  @Override
  public void actionPerformed(ActionEvent e) {
    AttackFX attackfx = new AttackFX();

    this.add(attackfx, BorderLayout.CENTER);
    this.validate();
  }

  class AttackFX extends JPanel implements ActionListener {

    private static final long serialVersionUID = 1L;
    private int xCoor = 100;
    private Timer t;

    public AttackFX() {
        t = new Timer(10, this);
        t.start();
    }


    public void paintComponent ( Graphics graphics ) {
        super.paintComponent ( graphics );
        Graphics2D graphics2d = ( Graphics2D ) graphics;

        graphics.setColor ( Color.cyan );
        graphics2d.fillOval(xCoor, 40, 8, 8);
    }

    /*
     * The timer will fire an action every 10 milliseconds, moving
     * our circle by 1 each time.
     * 
     * I unfortunately had to hard code a value that the circle should stop at
     * but I am sure you can find a way around this.
     */
    @Override
    public void actionPerformed(ActionEvent e) {

        if (xCoor > 250) {
            t.stop();
            setVisible(false);
        }

        xCoor++;
        repaint();
    }
  }
}

我不知道是不是setVisible()是最好的方法,但它比删除JPanel更安全。请告诉我您的想法。

以下是一些工作代码,当单击按钮时会生成一个圆圈,当它达到某个x值后会消失:

public class App extends JFrame implements ActionListener {

  private static final long serialVersionUID = 1L;

  public static void main(String[] a) {
    App app = new App();
    app.createControls();
  }

  public App() {
    setResizable(false);
    setTitle("OnFight.Combat_Arena_Beta");
    setSize(381, 400);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new BorderLayout());
  }

  private void createControls() {
    JPanel upper = new JPanel();

    JButton fire = new JButton("Attack");
    fire.addActionListener(this);
    upper.add(fire);

    this.add(upper, BorderLayout.NORTH);
    setVisible(true);
  }

  /*
   * When the "Attack" button is pressed the time will start and the
   * circle will start painting.
   */
  @Override
  public void actionPerformed(ActionEvent e) {
    AttackFX attackfx = new AttackFX();

    this.add(attackfx, BorderLayout.CENTER);
    this.validate();
  }

  class AttackFX extends JPanel implements ActionListener {

    private static final long serialVersionUID = 1L;
    private int xCoor = 100;
    private Timer t;

    public AttackFX() {
        t = new Timer(10, this);
        t.start();
    }


    public void paintComponent ( Graphics graphics ) {
        super.paintComponent ( graphics );
        Graphics2D graphics2d = ( Graphics2D ) graphics;

        graphics.setColor ( Color.cyan );
        graphics2d.fillOval(xCoor, 40, 8, 8);
    }

    /*
     * The timer will fire an action every 10 milliseconds, moving
     * our circle by 1 each time.
     * 
     * I unfortunately had to hard code a value that the circle should stop at
     * but I am sure you can find a way around this.
     */
    @Override
    public void actionPerformed(ActionEvent e) {

        if (xCoor > 250) {
            t.stop();
            setVisible(false);
        }

        xCoor++;
        repaint();
    }
  }
}

我不知道是不是setVisible()是最好的方法,但它比删除JPanel更安全。让我知道你的想法。

1)要更快地获得更好的帮助,请发布一个。2) 请在句子开头加一个大写字母。也可以用大写字母表示I,缩写和首字母缩略词如JEE或WAR。这使得人们更容易理解和帮助。“我没有太多时间。”我没有太多耐心。SSCCE在哪里?“好的,先生?”它没有编译<代码>1)要更快地获得更好的帮助,请发布一个。2) 请在句子开头加一个大写字母。也可以用大写字母表示I,缩写和首字母缩略词如JEE或WAR。这使得人们更容易理解和帮助。“我没有太多时间。”我没有太多耐心。SSCCE在哪里?“好的,先生?”它没有编译<代码>如何减少动画的闪烁效果?我将多次单击攻击按钮,当我继续单击按钮时,闪烁逐渐增强。之所以会出现闪烁效果,是因为当圆已经移动了一段距离时,您要求在(100,40)处重新绘制圆。所以它会在这个x值和另一个x值之间切换。昨晚我试图制作一些东西来阻止这一切,但我找不到任何东西来在同一个JPanel上创建多个圆,甚至无法动态生成JPanel。如何减少动画的闪烁效果?我将多次单击攻击按钮,当我继续单击按钮时,闪烁逐渐增强。之所以会出现闪烁效果,是因为当圆已经移动了一段距离时,您要求在(100,40)处重新绘制圆。所以它会在这个x值和另一个x值之间切换。昨晚我试图做一些事情来阻止这一点,但我找不到任何东西来在同一个JPanel上创建多个圆,甚至无法动态生成JPanel。