Results 1 to 5 of 5

Thread: Animated gif

  1. #1
    tamoti is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Question Animated gif

    I need to add an animated gif in my Java project. I found this code on the forum and it works perfectly, but I don't want to use a URL, instead I want to add the image with a path in my program. This code is also used in the testclass and I want to use it in a class that doesn't have a public static void main. How can I do this, I've been trying and trying and trying but it only gets worse .

    Java Code:
    import java.awt.*;
    import java.net.*;
    import javax.swing.*;
      
    public class Anima {
        public static void main(String[] args) throws MalformedURLException {
     
            URL url = new URL("<URL to your Animated GIF>");
            Icon icon = new ImageIcon(url);
            JLabel label = new JLabel(icon);
      
            JFrame f = new JFrame("Animation");
            f.getContentPane().add(label);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        }
    }

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Animated gif

    Use getClass().getResouce("name of file relative to package of this class") to retrieve the URL to a file within a package in your project. See the API for these methods to find out proper usage. If you want to find an image outside your jar/package folders, create a File, then use the toURI().toURL() (again, I recommend reading the API of these methods for more information)

  3. #3
    tamoti is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Animated gif

    While waiting for an answer I was experimenting a bit and I can get the animation (working) in my project with following code:
    Java Code:
    private JLabel imageLabel = new JLabel();
    
    try{
    			ImageIcon ii = new ImageIcon(this.getClass().getResource(
                        "nyanCatAnimation.gif"));
    			imageLabel.setIcon(ii);
    			imageLabel.setBounds(384, 336, 132, 72);
    			add(imageLabel);
    		} catch (Exception ex){
    			System.out.println("de afbeelding kan niet laden");
    		}
    So indeed it works when I use getClass() and getResource().
    I also searched the API but I'm new to Java and I find the API site very foreign for a newby.
    With solving this problem I got a new one and it fits in this thread so I think it's most easy if I post it here:

    I noticed that when I run the program the animated gif flickers, It doesn't play smooth (black interuptions). Can I fix this?
    Srry for my bad english

  4. #4
    tamoti is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Default Re: Animated gif

    I found out you can get rid of the flickering when you buffer the image, but I don't understand the buffering
    (I've checked the API, google and youtube for buffering image information, but not one understandable (good) result)
    Then I found this code to buffer an image:
    Java Code:
    public void update (Graphics g) 
    		{
    		// initialize buffer 
    		if (dbImage == null) 
    		{
    		dbImage = createImage (this.getSize().width, this.getSize().height); 
    		dbg = dbImage.getGraphics (); 
    		} 
    
    		// clear screen in background 
    		dbg.setColor (getBackground ()); 
    		dbg.fillRect (0, 0, this.getSize().width, this.getSize().height); 
    
    		// draw elements in background 
    		dbg.setColor (getForeground()); 
    		paint (dbg); 
    
    		// draw image on the screen 
    		g.drawImage (dbImage, 0, 0, this); 
    		}
    But I don't understand how to combine this code with the code from my previous message, where do I put it and do I have to change anything to it?

  5. #5
    tamoti is offline Member
    Join Date
    Nov 2011
    Posts
    4
    Rep Power
    0

    Lightbulb Re: Animated gif

    The problem is solved , I fixed it by accident , I know it sounds weird but I was experimenting a bit and suddently I run my program and the animated gif displays smooth.
    These are my code changes (I didn't had to use buffered image, kinda strange).

    Java Code:
    public void update (Graphics g) {
    		try{
    			ImageIcon nyan = new ImageIcon(this.getClass().getResource(
    	                   "nyanCatAnimation.gif"));
    			imageLabel.setIcon(nyan);
    			imageLabel.setBounds(384, 336, 132, 72);
    			imageLabel.setVisible(true);
    			add(imageLabel);
    		} catch (Exception ex){
    			g.drawString("nyan cat can't be displayed", 0, 710);
    		}
    		}
    So I've just put my image in a public void update.
    I make the image visible with a button and all I had to do was to put two lines of code under my actionHandler:
    update(getGraphics());
    repaint();

    There's also an if-statement to change that when you press the button the image becomes visible and if u press it again it gets unvisible, but I'm not gonna post that here I'm just saying incase you're wondering why I added imageLabel.setVisible(true)



    You probably understand by now that I'm new to Java. So if my solution is not good and buffering the image is really necessary then please tell me!
    Otherwise this thread can be closed and die a silent dead.

Similar Threads

  1. reset an animated gif?
    By totj in forum New To Java
    Replies: 0
    Last Post: 06-30-2011, 06:56 AM
  2. Animation with Animated GIF
    By JavaBean in forum Java 2D
    Replies: 3
    Last Post: 06-04-2011, 05:26 PM
  3. Extracting jpg from animated gifs
    By Maz in forum Java 2D
    Replies: 3
    Last Post: 05-05-2011, 01:29 PM
  4. How to add MouseListener to animated applet?
    By crikey in forum New To Java
    Replies: 37
    Last Post: 08-11-2010, 09:08 PM
  5. Playing an Animated GIF
    By c0m4ndo45 in forum Java 2D
    Replies: 3
    Last Post: 04-24-2009, 11:47 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •