Results 1 to 5 of 5
Thread: Animated gif
- 11-03-2011, 08:56 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
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); } }
- 11-03-2011, 09:40 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,605
- Rep Power
- 5
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)
- 11-03-2011, 10:34 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
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:
So indeed it works when I use getClass() and getResource().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"); }
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
- 11-04-2011, 11:59 AM #4
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
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:
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?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); }
- 11-04-2011, 03:29 PM #5
Member
- Join Date
- Nov 2011
- Posts
- 4
- Rep Power
- 0
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).
So I've just put my image in a public void update.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); } }
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
-
reset an animated gif?
By totj in forum New To JavaReplies: 0Last Post: 06-30-2011, 05:56 AM -
Animation with Animated GIF
By JavaBean in forum Java 2DReplies: 3Last Post: 06-04-2011, 04:26 PM -
Extracting jpg from animated gifs
By Maz in forum Java 2DReplies: 3Last Post: 05-05-2011, 12:29 PM -
How to add MouseListener to animated applet?
By crikey in forum New To JavaReplies: 37Last Post: 08-11-2010, 08:08 PM -
Playing an Animated GIF
By c0m4ndo45 in forum Java 2DReplies: 3Last Post: 04-24-2009, 10:47 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks