Link to home
Start Free TrialLog in
Avatar of James Hancock
James HancockFlag for United States of America

asked on

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()  ?
Avatar of for_yan
for_yan
Flag of United States of America image

Get frammes from animated GIF:
http://www.java2s.com/Tutorial/Java/0419__PDF/GetFramefromAnimatedGif.htm

import java.io.FileOutputStream;

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"));
    document.open();
    document.add(Image.getInstance("animated.gif"));
    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;) {
      ++i;
      document.add(img.getImage(i));
    }
    document.close();
  }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of for_yan
for_yan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of James Hancock

ASKER

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
http://www.javalobby.org/articles/ultimate-image/
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:
http://www.java2s.com/Code/Java/2D-Graphics-GUI/ConvertjavaawtimageBufferedImagetojavaawtImage.htm

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   
        frame.setVisible(true);   
        // 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?



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
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.
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
ID:36951481
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
see http://download.oracle.com/javase/1.4.2/docs/api/java/awt/image/BufferedImage.html
thx
You are always welcome.