package uk.ac.leeds.ccg.andyt.projects.genesis.gui; import java.applet.Applet; import java.awt.Graphics; import java.awt.Image; import java.awt.MediaTracker; import java.io.File; import java.net.URL; public class AnimationApplet extends Applet { Image[] img; int index = 0; int maxImg; MediaTracker tracker; public void init() { File _Directory = new File("C:/temp/test/"); String[] _Filenames = _Directory.list(); img = new Image[_Filenames.length]; // 2 images in animation maxImg = img.length - 1; tracker = new MediaTracker(this); try { // images loading for (int i = 0; i < _Filenames.length; i++) { img[i] = getImage(new URL(getDocumentBase(), "test/" + _Filenames[i])); //System.out.println(new URL(getDocumentBase(), "test/" + _Filenames[i])); tracker.addImage(img[i], i); } 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 { AnimationApplet animationApplet; int delay; public void delayedAnimation(AnimationApplet a, int delay) { this.animationApplet = a; this.delay = delay; } @Override public void run() { while (true) { try { animationApplet.resize( img[0].getWidth(animationApplet), img[0].getHeight(animationApplet)); sleep(delay); animationApplet.animate(); } catch (Exception e) { e.printStackTrace(); } } } } }