Toggle your browser's Back and Forward buttons to compare the running applet to this code. |
Click here to run a fresh copy of the applet: AnimatorApplet5.html |
import java.awt.*;
import java.applet.Applet;
/** EXTEND THE JAVA APPLET CLASS AND IMPLEMENT THE RUNNABLE INTERFACE **/
public class AnimatorApplet5 extends Applet implements Runnable
{int frameNumber = -1;
int delay;
Thread animatorThread;
boolean frozen = false;
Font font=new Font("TimesRoman",Font.BOLD,24);
FontMetrics fontm = getFontMetrics(font);
String framestr = "";
Dimension preImageDim;
// Define preImage variables needed to
Image preImage;
// create the offscreen copy of the image
Graphics preImageGraphics;
// we want to paint on the screen.
Image gifImage[];
// Define an array of gif images and
MediaTracker tracker;
// a tracker to preload them.
int HiNumber;
Toggle your browser's Back and Forward buttons to compare the running applet to this code. |
Click here to run a fresh copy of the applet: AnimatorApplet5.html |
public void init()
{setBackground(Color.magenta);
String str;
int fps = 10;
// Define the default fps (frames per second).
str = getParameter("fps");
// Possibly, get HTML string specifying the
try
// fps.
{if (str != null)
{fps =
Integer.parseInt(str);} // If possible, convert HTML fps to an
}
// integer.
catch (Exception e) {}
// In case the try caused something strange.
delay = (fps > 0) ? (1000/fps):
100; // Convert fps to the milisecond delay
// time between frames.
gifImage = new Image[10];
tracker = new MediaTracker(this);
// Spawn the tracker background thread(s)
for (int i = 1; i <=
10; i++)
// in order to preload the images.
{gifImage[i-1] = getImage(getCodeBase(),
"images/T"+i+".gif");
tracker.addImage(gifImage[i-1],0);
}
}
Toggle your browser's Back and Forward buttons to compare the running applet to this code. |
Click here to run a fresh copy of the applet: AnimatorApplet5.html |
Toggle your browser's Back and Forward buttons to compare the running applet to this code. |
Click here to run a fresh copy of the applet: AnimatorApplet5.html |
public void run()
{try
// Start loading the images and
{tracker.waitForAll();}
// wait until done before starting
catch (InterruptedException
e) {}
// the animation.
Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
// Set the priority low.
long startTime = System.currentTimeMillis();
// Remember start time.
while (Thread.currentThread()
== animatorThread)
// The animation loop.
{frameNumber++;
// Advance the frame number.
repaint();
// Display the frame.
try
// Sleep for the delay period.
{startTime += delay;
Thread.sleep(Math.max(0,
startTime-System.currentTimeMillis()));
}
catch (InterruptedException e) {break;}
// In case the try caused
}
// something strange, exit
}
// the while loop.
Toggle your browser's Back and Forward buttons to compare the running applet to this code. |
Click here to run a fresh copy of the applet: AnimatorApplet5.html |
Toggle your browser's Back and Forward buttons to compare the running applet to this code. |
Click here to run a fresh copy of the applet: AnimatorApplet5.html |
public void stop()
{animatorThread = null;
// The user either clicked to stop or left the page,
preImageGraphics = null;
// so wipe out the current thread and preImage context.
preImage = null;
}
}