VliegendeKop.java
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class VliegendeKop extends Applet
implements Runnable {
int frameNumber = -1;
int delay = 100;
Thread animatorThread;
static String fgFile = "v4_java_logo.gif";
Image fgImage;
public void init() {
//Get the image.
fgImage = getImage(getCodeBase(), fgFile);
}
public void start() {
if (animatorThread == null) {
animatorThread = new Thread(this);
}
animatorThread.start();
}
public void stop() {
animatorThread = null;
}
public void run() {
//Just to be nice, lower this thread's priority
//so it can't interfere with other processing going on.
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
//Remember the starting time.
long startTime = System.currentTimeMillis();
//Remember which thread we are.
Thread currentThread = Thread.currentThread();
//This is the animation loop.
while (currentThread == animatorThread) {
//Advance animation frame.
frameNumber++;
//Display and progress.
repaint();
//Delay depending on how far we are behind.
try {
startTime += delay;
Thread.sleep(Math.max(0,
startTime-System.currentTimeMillis()));
} catch (InterruptedException e) {
break;
}
}
}
//Draw the current frame of animation.
public void paint(Graphics g) {
int compWidth = getWidth();
int compHeight = getHeight();
int imageWidth, imageHeight;
//If we have a valid width and height for the
//foreground image, draw it.
imageWidth = fgImage.getWidth(this);
imageHeight = fgImage.getHeight(this);
if ((imageWidth > 0) && (imageHeight > 0)) {
g.drawImage(fgImage,
((frameNumber*5)
% (imageWidth + compWidth))
- imageWidth,
(compHeight - imageHeight)/2,
this);
}
}
}