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/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
Java

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
CEHJ

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

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

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
mccarl

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

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
jb1dev

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.
mccarl

Ah, I see jb1dev got in while I was away from the keyboard! (I should refresh the page more often!)
ASKER
Kyle Hamilton

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 :)
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
jb1dev

How do you mean "find the interface" ?

The javadoc is here:
http://docs.oracle.com/javase/7/docs/api//javax/swing/ImageIcon.html
ASKER
Kyle Hamilton

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.
jb1dev

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?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
jb1dev

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?
jb1dev

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.
ASKER
Kyle Hamilton

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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
CEHJ

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
ASKER
Kyle Hamilton

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.
CEHJ

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 ;)
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER
Kyle Hamilton

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?
CEHJ

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