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

时间:2013-10-20 09:54:00

标签: java swing timer

enter image description here

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

}
}

我真的需要你的帮助。明天是我正在制作的整个节目的演示。这是完成它的最后一步。我尽我所能来解决这个问题,但没有成功。

1 个答案:

答案 0 :(得分:1)

这是一些工作代码,当点击按钮时会生成一个圆圈,并在它达到某个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更安全。让我知道你的想法。