Ive created a memory game which has a grid of JButtons, when the button is clicked it changes the ImageIcon background to an animation which shows the front of the card.
The problem is that when the button is clicked, the gif doesn't begin playing until the mouse is moved off the button and if the mouse is moved back on before the animation is finished it causes the animation to restart. Has anyone encountered this problem before? Or can think of a workaround.

thanks

Seems like you are working on bydefault class of JBUTTON, you can customize classes in java according to your needs...

AS JBUTTON implements imageobserver class you better check you desired function and overwrite it according to your own needs.

Thanks, would i need to remove the implementation of image observer for this to work? also whats the best way to go about this?

Just write your own function, it will be overwritten automatically. Search the necessary function which you want to edit.

maybe

import javax.swing.*;
import java.net.URL;

class ShowAnimatedGif {

    public static void main(String[] args) throws Exception {
        final URL url = new URL("http://pscode.org/media/starzoom-thumb.gif");
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //JLabel l = new JLabel(new ImageIcon(url));
                //JOptionPane.showMessageDialog(null, l);
                JButton btn = new JButton(new ImageIcon(url));
                JOptionPane.showMessageDialog(null, btn);
            }
        });
    }
}

Edited 4 Years Ago by mKorbel: n/a

This article has been dead for over six months. Start a new discussion instead.