Run Method of Thread Class Plays the Animation by calling Repaint
// The run() method is called when the applet's thread is started.
// This starts display of animation images by updating frame number then calling repaint.
// The run() method also loads and plays the sound file connected to the animation.
public void run()
{
try
{
// Set the frame number to 0
m_nFrameNumber = 0;
// force a repaint
repaint();
// force all images to be loaded first
m_mediaTracker.waitForAll();
// resize screen to match loaded image size
resize(m_frame[0].getWidth(null), m_frame[0].getHeight(null));
// Set up sleep delay time based on frame rate
int nDelay = 1000/m_fps;
// Get audio clip that goes with the animation
String sSoundClip = new String(m_imageFile + ".au");
AudioClip jabber = getAudioClip(getCodeBase( ), sSoundClip);
// Play the audio clip
jabber.play( );
while (true)
{
repaint();
Thread.sleep(nDelay);
}
}
catch (InterruptedException e)
{
stop();
}
}
Paint Method checks with MediaTracker if all Images are Loaded. It also
checks if we've reached the Last Frame by Comparing m_nFrameNumber with
m_numIages. If not the Paint Method Draws the next Frame using g.drawImage
// Javamation Paint Handler
public void paint(Graphics g)
{
if (m_mediaTracker.checkAll())
{
// see if there was an error
if (m_mediaTracker.isErrorAny())
{
// report the error if any
g.drawString("Error loading image", 100, 120);
return;
}
// draw the current frame
g.drawImage(m_frame[m_nFrameNumber], 0, 0, null);
// update the frame number until the last frame is reached
m_nFrameNumber++;
if (m_nFrameNumber > m_numImages)
{
m_nFrameNumber = 0;
}
}
else
{
g.drawString("Loading Images...", 100, 100);
}
}
Learn More
The complete applet source code.