home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-07-02 | 4.7 KB | 184 lines |
- // AnimationMT - this version of Animation uses the MediaTracker
- // to monitor the progress of loading the images
- import java.applet.*;
- import java.awt.*;
-
- public class AnimationMT extends Applet implements Runnable
- {
- Thread m_AnimationMT = null;
-
- private int m_fps = 10;
- private String m_imageFile = "LJN";
- private int m_numImages = 22;
-
- // Parameter names. To change a name of a parameter, you need only make
- // a single change. Simply modify the value of the parameter string below.
- //--------------------------------------------------------------------------
- private final String PARAM_fps = "fps";
- private final String PARAM_imageFile = "imageFile";
- private final String PARAM_numImages = "numImages";
-
- // use a media tracker to monitor progress
- MediaTracker m_mediaTracker = new MediaTracker(this);
-
- // store frame information here
- int m_nFrameNumber = 0;
- Image m_frame[];
-
- public AnimationMT()
- {
- // TODO: Add constructor code here
- }
-
- public String getAppletInfo()
- {
- return "Name: AnimationMT\r\n" +
- "Author: Stephen R. Davis\r\n" +
- "Created for Learn Java Now (c)";
- }
-
- public String[][] getParameterInfo()
- {
- String[][] info =
- {
- { PARAM_fps, "int", "Frame rate" },
- { PARAM_imageFile, "String", "Name of file seq" },
- { PARAM_numImages, "int", "Number of images" },
- };
- return info;
- }
-
- public void init()
- {
- String param;
-
- param = getParameter(PARAM_fps);
- if (param != null)
- m_fps = Integer.parseInt(param);
-
- param = getParameter(PARAM_imageFile);
- if (param != null)
- m_imageFile = param;
-
- param = getParameter(PARAM_numImages);
- if (param != null)
- m_numImages = Integer.parseInt(param);
-
- // If you use a ResourceWizard-generated "control creator" class to
- // arrange controls in your applet, you may want to call its
- // CreateControls() method from within this method. Remove the following
- // call to resize() before adding the call to CreateControls();
- // CreateControls() does its own resizing.
- //----------------------------------------------------------------------
- resize(320, 240);
-
- // TODO: Place additional initialization code here
- // Load images
- m_frame = new Image[m_numImages];
- String sImageFile;
- int nImageNum;
- for (int i = 0; i < m_numImages; i++)
- {
- nImageNum = i + 1;
- sImageFile = m_imageFile +
- ((nImageNum < 10)?"0":"") +
- nImageNum +
- ".gif";
- m_frame[i] = getImage(getDocumentBase(), sImageFile);
-
- // add this frame to the media tracker
- m_mediaTracker.addImage(m_frame[i], 0);
- }
- }
-
- public void destroy()
- {
- // TODO: Place applet cleanup code here
- }
-
- public void paint(Graphics g)
- {
- if (m_mediaTracker.checkAll())
- {
- // if there was an error...
- if (m_mediaTracker.isErrorAny())
- {
- // ...signal the error
- g.drawString("Error loading an image", 10, 20);
- return;
- }
-
- // draw the current frame
- g.drawImage(m_frame[m_nFrameNumber], 0, 0, null);
-
- // update the frame number
- m_nFrameNumber++;
- if (m_nFrameNumber >= m_numImages)
- {
- m_nFrameNumber = 0;
- }
- }
- else
- {
- g.drawString("Loading images...", 10, 20);
- }
- }
-
- public void update(Graphics g)
- {
- paint(g);
- }
-
- public void start()
- {
- if (m_AnimationMT == null)
- {
- m_AnimationMT = new Thread(this);
- m_AnimationMT.start();
- }
- // TODO: Place additional applet start code here
- }
-
- public void stop()
- {
- if (m_AnimationMT != null)
- {
- m_AnimationMT.stop();
- m_AnimationMT = null;
- }
-
- // TODO: Place additional applet stop code here
- }
-
- public void run()
- {
- try
- {
- // set the frame number to zero
- m_nFrameNumber = 0;
-
- // force a repaint
- repaint();
-
- // now force the images to be loaded
- m_mediaTracker.waitForAll();
-
- // resize the screen to match the images
- resize(m_frame[0].getWidth(null),
- m_frame[0].getHeight(null));
-
- // calculate delay time based on frame rate
- int nDelay = 1000 / m_fps;
- while (true)
- {
- repaint();
- Thread.sleep(nDelay);
- }
- }
- catch (InterruptedException e)
- {
- stop();
- }
- }
- }
-