/** * Copyright (c): Uwe Schmidt, FH Wedel * * You may study, modify and distribute this source code * FOR NON-COMMERCIAL PURPOSES ONLY. * This copyright message has to remain unchanged. * * Note that this document is provided 'as is', * WITHOUT WARRANTY of any kind either expressed or implied. */ import java.applet.Applet; import java.awt.*; import java.net.*; //-------------------- public class ImageAnimation extends Applet implements Runnable { //-------------------- Image [] images; int currImage = 0; int direction = +1; int delay = 500; //-------------------- public void init() { // applet Parameter einlesen int noOfImages = Integer.parseInt(getParameter("noOfImages")); URL documentBase = getDocumentBase(); String imageName = getParameter("image"); try { delay = Integer.parseInt(getParameter("delay")); } catch (Exception e) { } // Bilder laden showStatus(noOfImages + " to load"); images = new Image[noOfImages]; for (int i = 0; i < noOfImages; ++i) { String n = imageName + i + ".gif"; showStatus("loading image " + imageName); images[i] = getImage(documentBase,n); } } //-------------------- public void destroy() { // Bilder wegwerfen for (int i = 0; i < images.length; ++i) { images[i].flush(); } } //-------------------- public void paint(Graphics g) { showStatus("drawing image " + currImage); g.drawImage(images[currImage],0,0,this); } public void update(Graphics g) { paint(g); } //-------------------- // der animator thread Thread animatorThread; public void start() { // animator thread erzeugen // und starten if ( animatorThread == null ) { animatorThread = new Thread(this); } animatorThread.start(); } //-------------------- public void stop() { // animator thread vernichten animatorThread = null; } //-------------------- // das run interface public void run() { while (Thread.currentThread() == animatorThread) { currImage += direction; if (currImage == images.length || currImage == -1) { direction = -direction; currImage += 2 * direction; } repaint(); sleepAWhile(); } } //-------------------- // eine zeitlang nichts tun protected void sleepAWhile() { try { animatorThread.sleep(delay); } catch (InterruptedException e) { } } } //--------------------