Link to home
Start Free TrialLog in
Avatar of Kyle Hamilton
Kyle HamiltonFlag for United States of America

asked on

gif animation inside an icon

further to this post:

https://www.experts-exchange.com/questions/28305132/loading-a-file-inside-a-Java-package.html

I'd like to know if there is a way to preserve the gif animation in an icon.

here's how I load my icon:

URL dancing = getClass().getResource("/img/dancingCat.gif");
BufferedImage img2 = ImageIO.read(dancing);

Is a BufferedImage a processed image? meaning does it process all the pixels in dancingCat.gif and recreate the image? Is that why it's not animated anymore? Am I talking shite?

How would I load up an animated gif preserving the animation?

Thanks,
Koza
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

afaik animated gifs aren't supported. You'd have to do your own animation
Avatar of jb1dev
jb1dev

This is not correct. A JLabel will do this.
ASKER CERTIFIED SOLUTION
Avatar of jb1dev
jb1dev

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Swing (at least in Java 6) does support animated GIF's. This simple example works fine for me...
import java.awt.Dimension;
import java.net.URL;

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

public class TestAnimatedGif extends JFrame {
    
    public TestAnimatedGif() {
        super("Test Animated GIF");
        
        setPreferredSize(new Dimension(640, 480));
        
        JPanel panel = new JPanel();
        add(panel);
        
        URL location = getClass().getResource("/animated.gif");
        JLabel label = new JLabel(new ImageIcon(location));
        
        panel.add(label);
        
        pack();
    }
    
    public static void main(String[] args) {
        new TestAnimatedGif().setVisible(true);
    }
    
}

Open in new window

Sorry maybe I have misunderstood.
If your requirement is to use a BufferedImage than I am not clear on that.

I meant to say that the ImageIcon class supports animated gifs.

But maybe that is not the answer you are looking for and I'm just confused.
Ah, I see jb1dev got in while I was away from the keyboard! (I should refresh the page more often!)
Avatar of Kyle Hamilton

ASKER

that's awesome. I just have to figure out how to incorporate it into my code. I was using Icon. This portion of the code was given to me. The person had a custom Icon implementation such that I can swap the image based on some result. I just have to find the ImageIcon interface..

If I can't figure it out, I'll be back :)
How do you mean "find the interface" ?

The javadoc is here:
http://docs.oracle.com/javase/7/docs/api//javax/swing/ImageIcon.html
so what I have now, works with a BufferedImage:

private class MyIcon implements Icon {
                        @Override
			public void paintIcon(Component c, Graphics g, int x, int y) {
				Graphics2D g2d = (Graphics2D) g.create();

				try{

					URL dancing = getClass().getResource("/img/dancingCat.gif");
					BufferedImage img2 = ImageIO.read(dancing);

					URL grumpy = getClass().getResource("/img/grumpycat.jpg");
	    			        BufferedImage img1 = ImageIO.read(grumpy);

                                        if(result){
						g2d.drawImage(img2, null, 0,0);
					}else{
						g2d.drawImage(img1, null, 0,0);
					}
		
				}catch(IOException e){
					e.printStackTrace();
				}
				g2d.dispose();
				
		       
			}
}

Open in new window


It want to have something that does something like:

private class MyIcon implements Icon {
                       @Override
			public void setImage(Image img){
					URL grumpy = getClass().getResource("/img/grumpycat.jpg");
	    			        URL dancing = getClass().getResource("/img/dancingCat.gif");

					if(result){
						 ImageIcon img = new ImageIcon(dancing);
					}else{
						 ImageIcon img = new ImageIcon(grumpy);
					}
			}
}

Open in new window


Except, that's not right, for one because Image is not an ImageIcon, so I get a compiler error. Though it's probably just plain wrong.
Not sure about this, but if you intend to implement Icon yourself, you can look at how the ImageIcon does it by looking at the jdk source.

Check

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/javax/swing/ImageIcon.java

Note

Sets the image observer for the image. Set this property if the ImageIcon contains an animated GIF, so the observer is notified to update its display. For example:

     icon = new ImageIcon(...)
     button.setIcon(icon);
     icon.setImageObserver(button);
 

Also is there any reason to re-implement Icon yourself rather than extend ImageIcon which seems to already handle what you want?
I don't understand you code here
			public void setImage(Image img){
					URL grumpy = getClass().getResource("/img/grumpycat.jpg");
	    			        URL dancing = getClass().getResource("/img/dancingCat.gif");

					if(result){
						 ImageIcon img = new ImageIcon(dancing);
					}else{
						 ImageIcon img = new ImageIcon(grumpy);
					}
			}

Open in new window


What is the purpose of this?
"result" isn't defined. And you are setting a method local variable "img" which does nothing, as it is not a member variable, and the method returns void and you're bnot returning anything. So to the method caller, those are basically NOOPs.

Can you post a full, compilable example of what you are trying to do?
Note the default impl of paintIcon() which makes use of the observer when set, and comments indicate this is needed for animated gifs.

    public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
        if(imageObserver == null) {
           g.drawImage(image, x, y, c);
        } else {
           g.drawImage(image, x, y, imageObserver);
        }
    }

Open in new window


And if you are overriding setImage() for whatever reason, this is the default impl

    public void setImage(Image image) {
        this.image = image;
        loadImage(image);
    }

Open in new window


This is taken from the OpenJDK7 source, I'm not finding Oracle's impl at this moment. I'll look again for that later. But I'm guessing this is going to be very similar.
I'm sorry if what I posted was nonsense. I just wanted to be able to use an animated gif in place of the buffered image. I wasn't thinking of what I was writing.

'result' is defined elsewhere. so it's not the problem.

thank you for all the info though. I will read up on this. I ended up using a completely different solution to my problem, so this is no longer "urgent" :)

cheers.
Sorry - i have been using Java since it was invented and back in the day, they were not supported.
afaik animated gifs aren't supported.
(my emphasis)
Gifs are something i never use, animated or not, owing to licensing issues
Hi CEHJ,

If I use an ImageIcon, the gif is animated, so I guess in that sense, it is supported.  This is just a personal project and the animated gif is just a silly feature I wanted to add because I was procrastinating from doing the actual work that I need to do to make my program work... :)

I have the utmost of respect or your JAVA knowledge. Please forgive me if I'm being stupid noob :) I gotta start somewhere.
I'm sure you're not stupid in any way ;) It's good to be reminded that things do change in Java and that it's not good to make assumptions (not that i said categorically they were not supported :P)

fyi, at one time, loading of gifs by ImageIO (animated or not) was also not supported. I'll let you tell me if that has now changed if you're so inclined ;)
URL dancing = getClass().getResource("/img/dancingCat.gif");
BufferedImage img2 = ImageIO.read(dancing);

Open in new window


This works, though it does not animate. So yeah, I guess it's supported, :)

My understanding is that BufferedImage processes the pixels, and re-constitutes them, so to speak, so it makes sense that it wouldn't animate. It just takes the first frame.

Whereas an ImageIcon is a reference to the actual file?
A BufferedImage is a wrapper for a bitmap with extra methods. There's no actual visual element though until it is painted on a graphics context. The ImageIcon is a gui component though