4

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?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
se7en
  • 87
  • 1
  • 3
  • 16
  • @EranMedan :i think that he want to `start` and `stop` the animation , not just about to display it – Alya'a Gamal Apr 16 '13 at 14:36
  • I guess I am neither trying to just display the image nor having an issue with the frames, so these suggestions are not helping. – se7en Apr 16 '13 at 14:46
  • *"I want to display an animated (`.gif` preferably) image once I click the button."* GIF is the only format that supports animations. So saying 'preferably' just confuses people. I am guessing the important bit is 'animation'. Please confirm, and edit your question so it is more clear. – Andrew Thompson Apr 16 '13 at 14:51
  • 1
    `these suggestions are not helping.` Then maybe you should ask a clear question. People are trying their best with the information given. – camickr Apr 16 '13 at 14:52
  • Removing the word 'preferably' does not solve anything. – Andrew Thompson Apr 16 '13 at 14:54
  • I am concerned about an animated GIF image , that's all. @AndrewThompson, please help with the edit if you can think of some better words. – se7en Apr 16 '13 at 14:56

2 Answers2

8

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("https://i.stack.imgur.com/XUmOD.png")),
            ImageIO.read(new URL("https://i.stack.imgur.com/zKyiD.png")),
            ImageIO.read(new URL("https://i.stack.imgur.com/4maMm.png")),
            ImageIO.read(new URL("https://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);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
3

I don't know how to get the frame/images from a gif, but if you have access to them, then you can use the Animated Icon class to do the animation for you. It uses a Timer behind the scenes to do the animation so you can simply start/stop/pause the Timer as you wish.

camickr
  • 321,443
  • 19
  • 166
  • 288