Solved

gif animation inside an icon

Posted on 2013-11-27
18
487 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
Comment Utility
afaik animated gifs aren't supported. You'd have to do your own animation
0
 
LVL 14

Expert Comment

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

Accepted Solution

by:
jb1dev earned 500 total points
Comment Utility
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
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Expert Comment

by:jb1dev
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array6 challenfge 6 62
network + 7 72
groovy example issue 10 65
Unexpected HttpURLConnection connection behavior 2 46
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now