Can Java Image objects be paused or frames extracted?

I have animated GIF's in my game.

I'd like to be able to display one of the frames in the GIF, done in code, without making individual GIF files for the desired image / single frame.

Since I already have the image loaded (in the animation) can I make a new Image Object from one of the animation frames?

If not a new image object, can I tell an Image object to pause()  ?
Who is Participating?
for_yanConnect With a Mentor Commented:

I tested this one from
and extracted all  frames of
into separate gif files with this code:

import java.awt.image.BufferedImage;
import java.util.Iterator;
import javax.imageio.*;

public class ExtractingFrames {
public static void main(String[] args) throws IOException {
Object input = new File("animated.gif");
// or Object input = new FileInputStream("animated.gif");
ImageInputStream stream = ImageIO.createImageInputStream(input);
Iterator readers = ImageIO.getImageReaders(stream);
if (!readers.hasNext())
throw new RuntimeException("no image reader found");
ImageReader reader = (ImageReader);
reader.setInput(stream); // don't omit this line!
int n = reader.getNumImages(true); // don't use false!
System.out.println("numImages = " + n);
for (int i = 0; i < n; i++) {
BufferedImage image =;
 File outputfile = new File("images\\saved" + i + ".gif");
    ImageIO.write(image, "gif", outputfile);
System.out.println("image[" + i + "] = " + image);

Open in new window

Get frammes from animated GIF:


import com.lowagie.text.Document;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.codec.GifImage;

public class MainClass {
  public static void main(String[] args) throws Exception {
    Document document = new Document();
    PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));;
    GifImage img = new GifImage("animated.gif");
    int frames = img.getFrameCount();
    document.add(new Paragraph("There are " + frames + " frames in the animated gif file."));
    for (int i = 0; i < frames;) {

Open in new window

See the code above for how to make individual images without writing them to files - they are of course forst extracted
and then written to files.
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

beavoidAuthor Commented:
How can I change the last for loop to return the k-th image as an Image object, not as a bufferedImage ?

Can I "cast" a bufferedimage  to an Image Object?

I don't need to write it to an external file, I need it loaded in to an Image Object right away

// in last for loop above
if (i==k)    return Toolkit.getDefaultToolkit().createImage(bufferedImage.getSource());


Will that be, as if, extracting a paused image?
This gives example from buffered image to image drawing
beavoidAuthor Commented:
It looks like that is only relevant for BufferredImage objects. not plain Image objects

Mine are regular awt Image objects

Is there a bufferedimage extraction capability from an Image object?
and also, then buffered image back to Image ?

Cause, then I can use that code, no problem

I think this converts Image to BufferedImage:

and this is drawing image from BufferedImage from that link above:
   BufferedImage loadImg = ImageUtil.loadImage("C:/Images/duke.gif");   
        frame.setBounds(0, 0, loadImg.getWidth(), loadImg.getHeight());   
        // Set the panel visible and add it to the frame   
        // Get the surfaces Graphics object   
        Graphics2D g = (Graphics2D)frame.getRootPane().getGraphics();   
        // Now draw the image   
        g.drawImage(loadImg, null, 0, 0);   

Open in new window

So from animated gif you can either write it to a file or
darrw it on java component, doesn't that cover your needs?

beavoidAuthor Commented:
for the last 3 lines, I can just use the getgraphics from my Image object, I guess.

Game over, no worrying about buffered image conversion or components?
I think so, irt seems you should be able to do it both ways
beavoidAuthor Commented:
Could you please give me a bit of text on that page to find the conversion of bufferedimage to image? - section? (ultimate image thing)

> give me a bit of text on that page

Sorry, don't understand whivh page you mean.
Please, explain.
beavoidAuthor Commented:
I can't find the exact code lines that do the conversions.

May I have a hint as to its place ( If I can search for text in that area, it would be good)

the code in
allows you to show image on your java component.
Try how it works.
I didn't try but hope it does work.
So you can write graphics file form BufferedImage, you can show it ion Java component.
What other needs do you have?
BufferedImage is actually subclass of Image.
Image is abstract class. So you cannot talke about conversion of BufferedImgae to Image.
BufferedImage IS Image
beavoidAuthor Commented:
You are always welcome.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.