
One type of Java animation is the the basic frame-based. It is said to be the simplest Java animation tricks. A common implementation of a Java frame animation is the the counter 1 sample applet.
The Counter1 sample applet.
// Counter1 Class
// Counter1.java
// Imports
import java.applet.*;
import java.awt.*;
public class Counter1 extends Applet implements Runnable {
Image< < numbers = new Image < 10 <
Thread animate;
MediaTracker tracker;
int frame = 0;
public void init< < {
// Load and track the images
tracker = new MediaTracker <this <
for <int i = 0; i < 10; i++< {
numbers[i] = getImage < getDocumentBase < <, "Res/" + i + ".gif" <
tracker.addImage(numbers[i], 0 <
}
}
public void start < < {
if < animate == null < {
animate = new Thread <this <
animate.start < <
}
}
public void stop < < {
if < animate != null< {
animate.stop < <
animate = null;
}
}
public void run< < {
try {
tracker.waitForID ɘ <
}
catch < InterruptedException e < {
return;
}
while < true < {
if < ++frame > 9 <
frame = 0;
repaint < <
}
}
public void paint <Graphics g < {
if < <tracker.statusID ɘ, true < & MediaTracker.ERRORED < != 0 < {
// Draw the error rectangle
g.setColor < Color.red <
g.fillRect ɘ, 0, size < <.width, size < <.height<
return;
}
if < < tracker.statusID ɘ, true < & MediaTracker.COMPLETE < != 0 < {
// Draw the frame image
g.drawImage < numbers[frame], 0, 0, this <
}
else {
// Draw the loading message
Font font = new Font < "Helvetica", Font.PLAIN, 16 <
FontMetrics fm = g.getFontMetrics < font <
String str = new String <"Loading images..." <
g.setFont < font <
g.drawString < str, < size < <.width - fm.stringWidth < str < / 2,
< <size < < .height - fm.getHeight < < < / 2 < + fm.getAscent< < <
}
}
}
end of sample
Comments