PDA

View Full Version : [JAVA] Immagini GIF


andrew1988
27-06-2008, 20:42
Ho un problema con le immagini gif.. visualizza solo il primo frame..

File immagine = new File("./img.gif");
Image img = ImageIO.read(immagine);
gra2D.drawImage(img, xPos, yPos, this);

come posso fare per visualizzare l'animazione completa?

wizard_at
28-06-2008, 10:41
dai un'occhiata a questo


/*<applet code=Animation width=67 height=48>
<param name=img value=horse.gif>
<param name=rows value=3>
<param name=cols value=4>
<param name=sequence value=0,1,2,3,4,5,6,7,8,9,10,11>
<param name=framerate value=15>
</applet>



import java.applet.*;

import java.awt.*;

import java.awt.image.*;

import java.util.*;



public class Animation extends Applet implements Runnable {

Image cell[];

final int MAXSEQ = 64;

int sequence[];

int nseq;

int idx;

int framerate;

boolean stopFlag;



private int intDef(String s, int def) {

int n = def;

if (s != null)

try {

n = Integer.parseInt(s);

} catch (NumberFormatException e) {

System.out.println("Number Format Exception");

}

return n;

}



public void init() {

framerate = intDef(getParameter("framerate"), 5);

int tilex = intDef(getParameter("cols"), 1);

int tiley = intDef(getParameter("rows"), 1);

cell = new Image[tilex*tiley];



StringTokenizer st = new

StringTokenizer(getParameter("sequence"), ",");

sequence = new int[MAXSEQ];

nseq = 0;

while(st.hasMoreTokens() && nseq < MAXSEQ) {

sequence[nseq] = intDef(st.nextToken(), 0);

nseq++;

}



try {

Image img = getImage(getDocumentBase(), getParameter("img"));

MediaTracker t = new MediaTracker(this);

t.addImage(img, 0);

t.waitForID(0);

int iw = img.getWidth(null);

int ih = img.getHeight(null);

int tw = iw / tilex;

int th = ih / tiley;

CropImageFilter f;

FilteredImageSource fis;

for (int y=0; y<tiley; y++) {

for (int x=0; x<tilex; x++) {

f = new CropImageFilter(tw*x, th*y, tw, th);

fis = new FilteredImageSource(img.getSource(), f);

int i = y*tilex+x;

cell[i] = createImage(fis);

t.addImage(cell[i], i);

}

}

t.waitForAll();

} catch (InterruptedException e) {

System.out.println("Image Load Interrupted");

}

}



public void update(Graphics g) { }



public void paint(Graphics g) {

g.drawImage(cell[sequence[idx]], 0, 0, null);

}



Thread t;

public void start() {

t = new Thread(this);

stopFlag = false;

t.start();

}



public void stop() {

stopFlag = true;

}



public void run() {

idx = 0;

while (true) {

paint(getGraphics());

idx = (idx + 1) % nseq;

try {

Thread.sleep(1000/framerate);

} catch (InterruptedException e) {

System.out.println("Animation Interrupted");

return;

}

if(stopFlag)

return;

}

}

}

andrew1988
28-06-2008, 12:35
quello è per un applet.. e per java? si fa la stessa cosa?

wizard_at
28-06-2008, 15:24
quello è per un applet.. e per java? si fa la stessa cosa?

e' un applet scritto in java. basta scorporare la parte che serve dall'applet.

usi swing o awt?

andrew1988
29-06-2008, 20:52
ho risolto utilizzando il toolkit... grazie mille x i suggerimenti