0 Members and 2 Guests are viewing this topic.
private void loadTiles(String name) { for (int i = 0; i < tileNames.length; i++) { try { BufferedImage image = ImageIO.read(TileSet.class.getResource("/tiles/" + name + "/" + tileNames[i] + ".gif")); Render result = new Render(xPix, yPix);//Render contains the pixel data in result.pixels System.out.println(result.width); image.getRGB(0, 0, xPix, yPix, result.pixels, 0, xPix); tileRender[i] = result; } catch (Exception e) { System.err.println("File: \"" + "tiles/" + name + "/" + tileNames[i] + ".gif" + "\" does not exist"); throw new RuntimeException(e); } }}
import java.awt.*;import java.awt.image.BufferedImage;import java.io.*;import java.util.Iterator;import javax.imageio.*;import javax.imageio.metadata.*;import javax.imageio.stream.*;import org.w3c.dom.*;public class GIFTEST { public static void main(String[] args) throws IOException { File input = new File("cat.gif"); ImageInputStream stream = ImageIO.createImageInputStream(input); Iterator readers = ImageIO.getImageReaders(stream); if (!readers.hasNext()) throw new RuntimeException("No image reader found"); ImageReader reader = (ImageReader)readers.next(); reader.setInput(stream); int num = reader.getNumImages(true); GifFrame[] frames = new GifFrame[num]; for (int i = 0; i < num; i++) { frames[i] = new GifFrame(); BufferedImage image = reader.read(i); frames[i].i = image; IIOMetadata meta = reader.getImageMetadata(i); NodeList nodes = meta.getAsTree(meta.getNativeMetadataFormatName()).getChildNodes(); for (int x = 0; x < nodes.getLength(); x++) { Node n = nodes.item(x); if (n.getLocalName().equals("GraphicControlExtension")) { NamedNodeMap atts = n.getAttributes(); frames[i].time = Integer.parseInt(atts.getNamedItem("delayTime").getNodeValue()); frames[i].dispose = atts.getNamedItem("disposalMethod").getNodeValue(); frames[i].wait = Boolean.parseBoolean(atts.getNamedItem("userInputFlag").getNodeValue()); } } } stream.close(); Frame f = new Frame("GIF Test"); GifAnimator ga = new GifAnimator(frames); ga.setSize(400, 400); f.add(ga); f.pack(); f.setVisible(true); ga.start(); }}class GifFrame { public Image i; public int time; public String dispose; public boolean wait;}class GifAnimator extends Canvas { private GifFrame[] frames; private Thread animator; private boolean run = false; private int f = 0; public GifAnimator(GifFrame[] frames) { this.frames = frames; } public void start() { run = true; animator = new Thread() { @Override public void run() { while (run) { for (int i = 0; i < frames.length; i++) { f = i; repaint(); try {sleep(frames[i].time * 10);} catch (Exception e) {} } } } }; animator.start(); } public void stop() { run = false; } @Override public void paint(Graphics g) { g.drawImage(frames[f].i, 0, 0, null); } @Override public void update(Graphics g) { paint(g); }}
Okay, here's an example I made that shows loading a gif and displaying it as an animation. You should be able to look at this and adapt it for what you need. If you need help, just ask, or if you want me to try and adapt it for you, I could try, you'll just need to provide a little more info on the code structure of the project =PCode: [Select]import java.awt.*;import java.awt.image.BufferedImage;import java.io.*;import java.util.Iterator;import javax.imageio.*;import javax.imageio.metadata.*;import javax.imageio.stream.*;import org.w3c.dom.*;public class GIFTEST { public static void main(String[] args) throws IOException { File input = new File("cat.gif"); ImageInputStream stream = ImageIO.createImageInputStream(input); Iterator readers = ImageIO.getImageReaders(stream); if (!readers.hasNext()) throw new RuntimeException("No image reader found"); ImageReader reader = (ImageReader)readers.next(); reader.setInput(stream); int num = reader.getNumImages(true); GifFrame[] frames = new GifFrame[num]; for (int i = 0; i < num; i++) { frames[i] = new GifFrame(); BufferedImage image = reader.read(i); frames[i].i = image; IIOMetadata meta = reader.getImageMetadata(i); NodeList nodes = meta.getAsTree(meta.getNativeMetadataFormatName()).getChildNodes(); for (int x = 0; x < nodes.getLength(); x++) { Node n = nodes.item(x); if (n.getLocalName().equals("GraphicControlExtension")) { NamedNodeMap atts = n.getAttributes(); frames[i].time = Integer.parseInt(atts.getNamedItem("delayTime").getNodeValue()); frames[i].dispose = atts.getNamedItem("disposalMethod").getNodeValue(); frames[i].wait = Boolean.parseBoolean(atts.getNamedItem("userInputFlag").getNodeValue()); } } } stream.close(); Frame f = new Frame("GIF Test"); GifAnimator ga = new GifAnimator(frames); ga.setSize(400, 400); f.add(ga); f.pack(); f.setVisible(true); ga.start(); }}class GifFrame { public Image i; public int time; public String dispose; public boolean wait;}class GifAnimator extends Canvas { private GifFrame[] frames; private Thread animator; private boolean run = false; private int f = 0; public GifAnimator(GifFrame[] frames) { this.frames = frames; } public void start() { run = true; animator = new Thread() { @Override public void run() { while (run) { for (int i = 0; i < frames.length; i++) { f = i; repaint(); try {sleep(frames[i].time * 10);} catch (Exception e) {} } } } }; animator.start(); } public void stop() { run = false; } @Override public void paint(Graphics g) { g.drawImage(frames[f].i, 0, 0, null); } @Override public void update(Graphics g) { paint(g); }}