0

I found a GIF I want to add into one of my Java programs. When I run it the picture shows up and works, but it leaves a trail behind it (it isn't actually replacing the white behind the picture) and the window is incorrectly sized. I searched before but nothing fixed it.

package tasks;

import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class duane
{
    public static void main(String[] args) throws MalformedURLException {
        URL url = new URL("http://i2.kym-cdn.com/photos/images/newsfeed/000/602/307/4d3.gif");
        Icon icon = new ImageIcon(url);
        JLabel label = new JLabel(icon);

        JFrame f = new JFrame("Animation");
        f.getContentPane().add(label);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}

What would I add in to make the window the correct size and how would I repaint it?

Jeffrey Bosboom
  • 13,313
  • 16
  • 79
  • 92
robbbbin
  • 13
  • 1
  • 1
  • 3
  • It's hard to 100% sure, but I would say that the dispose method used by the gif is not supported by Swing. For [example](http://stackoverflow.com/questions/26330550/java-gif-animation-not-repainting-correctly/26331052#26331052) – MadProgrammer Mar 10 '15 at 01:01
  • And in case you want to test a couple more gifs, this [example](http://stackoverflow.com/questions/23001292/animated-gif-leads-to-splashscreen-being-null/23001820#23001820) – MadProgrammer Mar 10 '15 at 01:11
  • Thanks for the insight! Tried both and for me they both worked. How would i change the dispose method? Or would i have to look online for another one? – robbbbin Mar 11 '15 at 00:45
  • You'll need to find a gif editor which would allow you to modify the dispose method... – MadProgrammer Mar 11 '15 at 00:51
  • any websites you know about? I can find any. – robbbbin Mar 11 '15 at 01:10
  • The few times I've done this, I use the gif writer linked in this [answer](http://stackoverflow.com/questions/18117283/mirroring-animated-gif-on-load-in-java-imageicon/18117326#18117326), but had to modify it slightly ;) – MadProgrammer Mar 11 '15 at 01:15

0 Answers0