Solved

gif animation inside an icon

Posted on 2013-11-27
18
514 Views
Last Modified: 2013-11-28
further to this post:

http://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
0
Comment
Question by:Kyle Hamilton
  • 7
  • 5
  • 4
  • +1
18 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 39682448
afaik animated gifs aren't supported. You'd have to do your own animation
0
 
LVL 14

Expert Comment

by:jb1dev
ID: 39682464
This is not correct. A JLabel will do this.
0
 
LVL 14

Accepted Solution

by:
jb1dev earned 500 total points
ID: 39682474
Example put together from a few google searches

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

public class Animation {
    
     public static void main(String[] args) throws Exception {

        ImageIcon imageIcon = 
            new ImageIcon(
                new URL("http://takelessons.com/blog/wp-content/uploads/2013/07/happy-cat.gif"));
        JLabel jLabel = new JLabel(imageIcon);

        JFrame frame = new JFrame();
        frame.getContentPane().add(jLabel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true); 
    }
}

Open in new window

0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 35

Expert Comment

by:mccarl
ID: 39682482
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

0
 
LVL 14

Expert Comment

by:jb1dev
ID: 39682484
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.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 39682488
Ah, I see jb1dev got in while I was away from the keyboard! (I should refresh the page more often!)
0
 
LVL 25

Author Closing Comment

by:Kyle Hamilton
ID: 39682492
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 :)
0
 
LVL 14

Expert Comment

by:jb1dev
ID: 39682499
How do you mean "find the interface" ?

The javadoc is here:
http://docs.oracle.com/javase/7/docs/api//javax/swing/ImageIcon.html
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39682517
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.
0
 
LVL 14

Expert Comment

by:jb1dev
ID: 39682642
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?
0
 
LVL 14

Expert Comment

by:jb1dev
ID: 39682647
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?
0
 
LVL 14

Expert Comment

by:jb1dev
ID: 39682652
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.
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39682663
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39683210
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
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39683586
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39683606
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 ;)
0
 
LVL 25

Author Comment

by:Kyle Hamilton
ID: 39683624
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?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 39684300
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
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JUnit 4 @Before and @BeforeClass differences 3 59
eclipse argument 14 60
egit plugin on eclipse 8 63
Facing this issue for maven proxy setting 2 11
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question