单击时 Java 摆动中的图像动画 [英] Image animation in java swing on click

查看:28
本文介绍了单击时 Java 摆动中的图像动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个 JFrame 和一个 JButton .单击按钮后,我想显示动画 (.gif) 图像.当另一个事件(比如 ActionEvent e)停止在 JFrame 中显示动画时.我的方法应该是什么?

Say I have a JFrame and a JButton in it. I want to display an animated (.gif) image once I click the button. While another event (say ActionEvent e) stops displaying the animation in the JFrame. What should be my approach?

推荐答案

JLabel 中显示第一个图像(动画帧).当用户单击按钮时,启动一个 Swing Timer,它将标签的图标更改为下一帧,在显示所有帧后循环.当用户再次点击按钮时,停止动画.

Display the 1st image (animation frame) in a JLabel. When the user clicks the button, start a Swing Timer that changes the icon of the label to the next frame(s), looping once it has shown all frames. When the user clicks the button again, stop the animation.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;

class Chomper {

    public static void main(String[] args) throws Exception {
        final Image[] frames = {
            ImageIO.read(new URL("http://i.stack.imgur.com/XUmOD.png")),
            ImageIO.read(new URL("http://i.stack.imgur.com/zKyiD.png")),
            ImageIO.read(new URL("http://i.stack.imgur.com/4maMm.png")),
            ImageIO.read(new URL("http://i.stack.imgur.com/wn9V5.png"))
        };
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JPanel gui = new JPanel(new BorderLayout());

                final JLabel animation = new JLabel(new ImageIcon(frames[0]));
                gui.add(animation, BorderLayout.CENTER);

                ActionListener animate = new ActionListener() {

                    private int index = 0;

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (index<frames.length-1) {
                            index++;
                        } else {
                            index = 0;
                        }
                        animation.setIcon(new ImageIcon(frames[index]));
                    }
                };
                final Timer timer = new Timer(200,animate);

                final JToggleButton b = new JToggleButton("Start/Stop");
                ActionListener startStop = new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (b.isSelected()) {
                            timer.start();
                        } else {
                            timer.stop();
                        }
                    }
                };
                b.addActionListener(startStop);
                gui.add(b, BorderLayout.PAGE_END);

                JOptionPane.showMessageDialog(null, gui);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

这篇关于单击时 Java 摆动中的图像动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆