Start, Stop, and Run methods of Thread class
// The start() method is called when the page containing the applet
// first appears on the screen. The initial implementation
// of this method starts execution of the applet's thread.
public void start()
{
if (m_Javamation == null)
{
m_Javamation = new Thread(this);
m_Javamation.start();
}
}
// The stop() method is called when the page containing the applet is
// no longer on the screen. The initial implementation of
// this method stops execution of the applet's thread.
public void stop()
{
if (m_Javamation != null)
{
m_Javamation.stop();
m_Javamation = null;
// Get audio clip with silence
AudioClip silence = getAudioClip(getCodeBase( ), "Silence.au");
silence.play();
}
else if (m_nFrameNumber == m_numImages)
{
m_Javamation.stop();
m_Javamation = null;
}
}
// The run() method is called when the applet's thread is started.
// This starts display of animation images.
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();
}
}
}
Learn More
The complete applet source code.