1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| import java.applet.*;
import java.awt.*;
public class AnimImages extends Applet implements Runnable {
Image [] img;
int cpt=1;
int nombreImage = 9;
Thread animation;
public void init() {
setBackground(Color.WHITE);
img = new Image[nombreImage];
for (int i = 1; i<nombreImage; i++) {
img[i] = getImage(getCodeBase(), "images/guepard" + i + ".gif");
}
animation = new Thread(this);
}
public void run() {
while (true) {
try {
cpt++;
if (cpt == nombreImage-1) cpt = 1;
repaint();
Thread.sleep(300);
}
catch (InterruptedException e) {}
}
}
public void start() {
animation = new Thread(this);
animation.start();
}
public void stop() {
animation.stop();
try {
animation.join();
}
catch (InterruptedException e) {}
}
public void paint(Graphics g) {
g.fillRect(0,0,200,150);
setBackground(Color.WHITE);
setForeground(Color.WHITE);
g.drawImage(img[cpt], 10, 15, this);
}
public void update(Graphics g){ //surcharge indispensable
paint(g);
}
} |
Partager