home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / Java++ / VJ / SAMPLES / JAVANOW / CHAP16 / ANIMATIONMT / ANIMATIONMT.JAVA < prev    next >
Encoding:
Java Source  |  1996-07-02  |  4.7 KB  |  184 lines

  1. // AnimationMT - this version of Animation uses the MediaTracker
  2. //               to monitor the progress of loading the images
  3. import java.applet.*;
  4. import java.awt.*;
  5.  
  6. public class AnimationMT extends Applet implements Runnable
  7. {
  8.     Thread     m_AnimationMT = null;
  9.  
  10.     private int m_fps = 10;
  11.     private String m_imageFile = "LJN";
  12.     private int m_numImages = 22;
  13.  
  14.     // Parameter names.  To change a name of a parameter, you need only make
  15.     // a single change.  Simply modify the value of the parameter string below.
  16.     //--------------------------------------------------------------------------
  17.     private final String PARAM_fps = "fps";
  18.     private final String PARAM_imageFile = "imageFile";
  19.     private final String PARAM_numImages = "numImages";
  20.  
  21.         // use a media tracker to monitor progress
  22.     MediaTracker m_mediaTracker = new MediaTracker(this);
  23.  
  24.     // store frame information here
  25.     int   m_nFrameNumber = 0;
  26.     Image m_frame[];
  27.  
  28.     public AnimationMT()
  29.     {
  30.         // TODO: Add constructor code here
  31.     }
  32.  
  33.     public String getAppletInfo()
  34.     {
  35.         return "Name: AnimationMT\r\n" +
  36.                "Author: Stephen R. Davis\r\n" +
  37.                "Created for Learn Java Now (c)";
  38.     }
  39.  
  40.     public String[][] getParameterInfo()
  41.     {
  42.         String[][] info =
  43.         {
  44.             { PARAM_fps, "int", "Frame rate" },
  45.             { PARAM_imageFile, "String", "Name of file seq" },
  46.             { PARAM_numImages, "int", "Number of images" },
  47.         };
  48.         return info;        
  49.     }
  50.  
  51.     public void init()
  52.     {
  53.         String param;
  54.  
  55.         param = getParameter(PARAM_fps);
  56.         if (param != null)
  57.             m_fps = Integer.parseInt(param);
  58.  
  59.         param = getParameter(PARAM_imageFile);
  60.         if (param != null)
  61.             m_imageFile = param;
  62.  
  63.         param = getParameter(PARAM_numImages);
  64.         if (param != null)
  65.             m_numImages = Integer.parseInt(param);
  66.  
  67.         // If you use a ResourceWizard-generated "control creator" class to
  68.         // arrange controls in your applet, you may want to call its
  69.         // CreateControls() method from within this method. Remove the following
  70.         // call to resize() before adding the call to CreateControls();
  71.         // CreateControls() does its own resizing.
  72.         //----------------------------------------------------------------------
  73.         resize(320, 240);
  74.  
  75.         // TODO: Place additional initialization code here
  76.         // Load images
  77.         m_frame = new Image[m_numImages];
  78.         String sImageFile;
  79.         int nImageNum;
  80.         for (int i = 0; i < m_numImages; i++)
  81.         {
  82.             nImageNum = i + 1;
  83.             sImageFile = m_imageFile +
  84.                          ((nImageNum < 10)?"0":"") + 
  85.                          nImageNum +
  86.                          ".gif";
  87.             m_frame[i] = getImage(getDocumentBase(), sImageFile);
  88.  
  89.             // add this frame to the media tracker
  90.             m_mediaTracker.addImage(m_frame[i], 0);
  91.         }
  92.     }
  93.  
  94.     public void destroy()
  95.     {
  96.         // TODO: Place applet cleanup code here
  97.     }
  98.  
  99.     public void paint(Graphics g)
  100.     {
  101.         if (m_mediaTracker.checkAll())
  102.         {
  103.            // if there was an error...
  104.            if (m_mediaTracker.isErrorAny())
  105.            {
  106.                // ...signal the error
  107.                g.drawString("Error loading an image", 10, 20);
  108.                return;
  109.            }
  110.  
  111.            // draw the current frame
  112.            g.drawImage(m_frame[m_nFrameNumber], 0, 0, null);
  113.  
  114.            // update the frame number
  115.            m_nFrameNumber++;
  116.            if (m_nFrameNumber >= m_numImages)
  117.            {
  118.                m_nFrameNumber = 0;
  119.            }
  120.         }
  121.         else
  122.         {
  123.             g.drawString("Loading images...", 10, 20);
  124.         }
  125.     }
  126.  
  127.     public void update(Graphics g)
  128.     {
  129.         paint(g);
  130.     }
  131.  
  132.     public void start()
  133.     {
  134.         if (m_AnimationMT == null)
  135.         {
  136.             m_AnimationMT = new Thread(this);
  137.             m_AnimationMT.start();
  138.         }
  139.         // TODO: Place additional applet start code here
  140.     }
  141.     
  142.     public void stop()
  143.     {
  144.         if (m_AnimationMT != null)
  145.         {
  146.             m_AnimationMT.stop();
  147.             m_AnimationMT = null;
  148.         }
  149.  
  150.         // TODO: Place additional applet stop code here
  151.     }
  152.  
  153.     public void run()
  154.     {
  155.         try
  156.         {
  157.             // set the frame number to zero
  158.             m_nFrameNumber = 0;
  159.  
  160.             // force a repaint
  161.             repaint();
  162.  
  163.             // now force the images to be loaded
  164.             m_mediaTracker.waitForAll();
  165.  
  166.             // resize the screen to match the images
  167.             resize(m_frame[0].getWidth(null),
  168.                    m_frame[0].getHeight(null));
  169.  
  170.             // calculate delay time based on frame rate
  171.             int nDelay = 1000 / m_fps;
  172.             while (true)
  173.             {
  174.                 repaint();
  175.                 Thread.sleep(nDelay);
  176.             }
  177.         }
  178.         catch (InterruptedException e)
  179.         {
  180.             stop();
  181.         }
  182.     }
  183. }
  184.