Do simple animation using ImagesTag(s): AWT
By using a Thread, we switch between 2 GIFs (
and
)
import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.net.*;
public class AnimationGifApplet extends Applet {
Image [] img;
int index = 0;
int maxImg;
MediaTracker tracker;
public void init() {
img = new Image[2]; // 2 images in animation
maxImg = img.length - 1;
tracker = new MediaTracker(this);
try {
// images loading
img[0] = getImage(new URL(getDocumentBase(), "images/gumby.gif"));
img[1] = getImage(new URL(getDocumentBase(), "images/gumby2.gif"));
tracker.addImage(img[0],0);
tracker.addImage(img[1],1);
tracker.waitForAll();
}
catch (Exception e) {
e.printStackTrace();
}
AnimationThread at = new AnimationThread();
at.delayedAnimation(this, 500);
at.start();
}
public void paint(Graphics g) {
if (img[0] != null) {
g.drawImage(img[index],0,0,this);
index = (index < maxImg) ? index + 1 : 0;
}
}
public void animate() {
repaint();
}
class AnimationThread extends Thread {
AnimationGifApplet animationApplet;
int delay;
public void delayedAnimation(AnimationGifApplet a, int delay) {
this.animationApplet = a;
this.delay = delay;
}
public void run() {
while (true) {
try {
sleep(delay);
animationApplet.animate();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
mail_outline
Send comment, question or suggestion to howto@rgagnon.com
Send comment, question or suggestion to howto@rgagnon.com