Ryan Dwyer
Ryan Dwyer

Reputation: 49

Java: drawImage animated gif is frozen on first frame

I got my code to draw my image in an applet, but it is an animated gif and it is stopped on the first frame as if it were a single image.

It is supposed to be the spooky scary skeleton dancing, but he's just standing still.

Here is my code:

import java.util.*;
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.*;

public class Spooky extends Applet
{
Image scary, trumpet, walking;
MediaTracker mt;
AudioClip spoopy;
Graphics buffer;
Image offscreen;
Dimension dim;

public void init()
{
setLayout(null);
mt = new MediaTracker(this);


mt.addImage(scary,1);
mt.addImage(trumpet,1);
mt.addImage(walking,1);
spoopy = getAudioClip(getDocumentBase(),"spoopy.wav");
spoopy.loop();
}

public void paint(Graphics g)
{
  try
{
    URL url = this.getClass().getResource("spooky.gif");
    BufferedImage img;
    img = ImageIO.read(url);
    mt.addImage(img,1);
    g.drawImage(img,0,0,300,300,this);
}
catch(IOException e)
{
}

}
}

Upvotes: 0

Views: 2874

Answers (3)

Cardinal System
Cardinal System

Reputation: 3430

I had a similar problem. Michail Michailidis's answer is one solution. However, if you are trying to load the GIF from an InputStream (which was the situation in my case) you will need a different solution. In this case, I would use the Toolkit class to load your image, more specifically, Toolkit#createImage(byte[]):

Image image;
try(InputStream stream = this.getClass().getResourceAsStream("/someImage.gif")) {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    int nRead;
    byte[] data = new byte[16384];

    while ((nRead = stream.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, nRead);
    }

    buffer.flush();

    image = Toolkit.getDefaultToolkit().createImage(buffer.toByteArray());
} catch (IOException e) {
    e.printStackTrace();
}

Upvotes: 0

Paul Samsotha
Paul Samsotha

Reputation: 209052

The problem is the ImageIO.read(url); method. Not sure how, but internally, it messes up the reading of the gif. Instead, construct an ImageIcon from the URL and use getImage() of the ImageIcon to get an Image

As an aside, don't load the image in the paint method. Load it in the init method.

public class Spooky extends Applet {
    Image image;

    public void init() {
        URL url = this.getClass().getResource("spooky.gif");
        image = new ImageIcon(url).getImage();
    }

    public void paint(Graphics g) {
        super.paint(g);
        g.drawImage(image, 0, 0, 300, 300, this);
    }  
}

Upvotes: 2

Michail Michailidis
Michail Michailidis

Reputation: 12191

I don't know if it helps but usually it is a problem of needing a separate Thread/Runnable for the animation and a separate for the rest of the code. At least that was a problem I had when I was making a small game. Try this one and let me know if it helps :)

Check this too: Displaying Gif animation in java

Update: An example I found (http://examples.oreilly.com/jswing2/code/) uses JApplet that supports gifs (Applet is the old one)

// AnimationApplet.java
// The classic animation applet rewritten to use an animated GIF.
//
import javax.swing.*;

public class AnimationApplet extends JApplet {
  public void init() {
    ImageIcon icon = new ImageIcon("images/rolling.gif");  // animated gif
    getContentPane().add(new JLabel(icon));
  }
}

Did you try not to put sound to see if animation works alone? It could be that sound needs a separate Runnable/Thread

Upvotes: 1

Related Questions