We help IT Professionals succeed at work.

gif animation inside an icon

Kyle Hamilton
on
1,124 Views
Last Modified: 2013-11-28
further to this post:

https://www.experts-exchange.com/Programming/Languages/Java/Q_28305132.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
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
afaik animated gifs aren't supported. You'd have to do your own animation

Commented:
This is not correct. A JLabel will do this.
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
mccarlIT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015

Commented:
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

Commented:
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.
mccarlIT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015

Commented:
Ah, I see jb1dev got in while I was away from the keyboard! (I should refresh the page more often!)
Kyle HamiltonData Scientist
Most Valuable Expert 2014

Author

Commented:
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 :)

Commented:
How do you mean "find the interface" ?

The javadoc is here:
http://docs.oracle.com/javase/7/docs/api//javax/swing/ImageIcon.html
Kyle HamiltonData Scientist
Most Valuable Expert 2014

Author

Commented:
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.

Commented:
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?

Commented:
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?

Commented:
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.
Kyle HamiltonData Scientist
Most Valuable Expert 2014

Author

Commented:
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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
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
Kyle HamiltonData Scientist
Most Valuable Expert 2014

Author

Commented:
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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
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 ;)
Kyle HamiltonData Scientist
Most Valuable Expert 2014

Author

Commented:
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?
CERTIFIED EXPERT
Top Expert 2016

Commented:
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
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.