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

  1. // Animation - this is a simple animation routine that
  2. //             replays the sequence of frames specified
  3. //             in the HTML page
  4. import java.applet.*;
  5. import java.awt.*;
  6.  
  7. public class Animation extends Applet implements Runnable
  8. {
  9.     Thread     m_Animation = null;
  10.  
  11.     private Graphics m_Graphics;
  12.     private Image     m_Images[];
  13.     private int      m_nCurrImage;
  14.     private int      m_nImgWidth  = 0;
  15.     private int      m_nImgHeight = 0;
  16.     private boolean  m_fAllLoaded = false;
  17.  
  18.     // rather than hard-code the number of images,
  19.     // get this information from the HTML page
  20.     // private final int NUM_IMAGES = 18;
  21.  
  22.     private int m_fps = 10;
  23.     private String m_imageFile = "LJN";
  24.     private int m_numImages = 22;
  25.  
  26.     // Parameter names.  To change a name of a parameter, you need only make
  27.     // a single change.  Simply modify the value of the parameter string below.
  28.     //--------------------------------------------------------------------------
  29.     private final String PARAM_fps = "fps";
  30.     private final String PARAM_imageFile = "imageFile";
  31.     private final String PARAM_numImages = "numImages";
  32.  
  33.     public Animation()
  34.     {
  35.         // TODO: Add constructor code here
  36.     }
  37.  
  38.     public String getAppletInfo()
  39.     {
  40.         return "Name: Animation\r\n" +
  41.                "Author: Stephen R. Davis\r\n" +
  42.                "Created for Learn Java Now (c)";
  43.     }
  44.  
  45.     public String[][] getParameterInfo()
  46.     {
  47.         String[][] info =
  48.         {
  49.             { PARAM_fps, "int", "Frame rate" },
  50.             { PARAM_imageFile, "String", "Name of the image seq" },
  51.             { PARAM_numImages, "int", "Number of images" },
  52.         };
  53.         return info;        
  54.     }
  55.  
  56.     public void init()
  57.     {
  58.         String param;
  59.  
  60.         param = getParameter(PARAM_fps);
  61.         if (param != null)
  62.             m_fps = Integer.parseInt(param);
  63.  
  64.         param = getParameter(PARAM_imageFile);
  65.         if (param != null)
  66.             m_imageFile = param;
  67.  
  68.         param = getParameter(PARAM_numImages);
  69.         if (param != null)
  70.             m_numImages = Integer.parseInt(param);
  71.  
  72.         // If you use a ResourceWizard-generated "control creator" class to
  73.         // arrange controls in your applet, you may want to call its
  74.         // CreateControls() method from within this method. Remove the following
  75.         // call to resize() before adding the call to CreateControls();
  76.         // CreateControls() does its own resizing.
  77.         //----------------------------------------------------------------------
  78.         resize(320, 240);
  79.  
  80.         // TODO: Place additional initialization code here
  81.     }
  82.  
  83.     public void destroy()
  84.     {
  85.         // TODO: Place applet cleanup code here
  86.     }
  87.  
  88.     public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h)
  89.     {
  90.         if (m_fAllLoaded)
  91.             return false;
  92.  
  93.         if ((flags & ALLBITS) == 0)
  94.             return true;
  95.  
  96.         if (++m_nCurrImage == m_numImages)
  97.         {
  98.             m_nCurrImage = 0;
  99.             m_fAllLoaded = true;
  100.         }                
  101.  
  102.         return false;
  103.     }
  104.  
  105.     private void displayImage(Graphics g)
  106.     {
  107.         if (!m_fAllLoaded)
  108.             return;
  109.  
  110.         // since I have resized the applet window to match
  111.         // the size of the image, there is no need to center
  112.         // the image
  113.         /*
  114.         g.drawImage(m_Images[m_nCurrImage], 
  115.                    (size().width - m_nImgWidth)   / 2,
  116.                    (size().height - m_nImgHeight) / 2, null);
  117.         */
  118.         g.drawImage(m_Images[m_nCurrImage], 0, 0, null);
  119.  
  120.         // the following puts the frame number up in the
  121.         // upper left hand corner - if you don't like it,
  122.         // just comment out the following two lines
  123.         g.setColor(Color.white);
  124.         g.drawString("Image #" + m_nCurrImage, 10, 20);
  125.     }
  126.  
  127.     public void paint(Graphics g)
  128.     {
  129.         // ANIMATION SUPPORT:
  130.         //        The following code displays a status message until all the
  131.         // images are loaded. Then it calls displayImage to display the current
  132.         // image.
  133.         //----------------------------------------------------------------------
  134.         if (m_fAllLoaded)
  135.         {
  136.             Rectangle r = g.getClipRect();
  137.             
  138.             g.clearRect(r.x, r.y, r.width, r.height);
  139.             displayImage(g);
  140.         }
  141.         else
  142.             g.drawString("Loading images...", 10, 20);
  143.  
  144.         // TODO: Place additional applet Paint code here
  145.     }
  146.  
  147.     public void start()
  148.     {
  149.         if (m_Animation == null)
  150.         {
  151.             m_Animation = new Thread(this);
  152.             m_Animation.start();
  153.         }
  154.         // TODO: Place additional applet start code here
  155.     }
  156.     
  157.     public void stop()
  158.     {
  159.         if (m_Animation != null)
  160.         {
  161.             m_Animation.stop();
  162.             m_Animation = null;
  163.         }
  164.  
  165.         // TODO: Place additional applet stop code here
  166.     }
  167.  
  168.     public void run()
  169.     {
  170.         repaint();
  171.  
  172.         m_Graphics = getGraphics();
  173.         m_nCurrImage   = 0;
  174.         m_Images   = new Image[m_numImages];
  175.  
  176.         String strImage;
  177.  
  178.         // For each image in the animation, this method first constructs a
  179.         // string containing the path to the image file; then it begins loading
  180.         // the image into the m_Images array.  Note that the call to getImage
  181.         // will return before the image is completely loaded.
  182.         //----------------------------------------------------------------------
  183.         for (int i = 1; i <= m_numImages; i++)
  184.         {
  185.             // strImage = "images/img00" +
  186.             //              ((i < 10) ? "0" : "") + i + ".gif";
  187.             strImage = m_imageFile + 
  188.                             ((i < 10) ? "0" : "") + i + ".gif";
  189.             m_Images[i-1] = getImage(getDocumentBase(), strImage);
  190.             if (m_nImgWidth == 0)
  191.             {
  192.                 try
  193.                 {
  194.                     // The getWidth() and getHeight() methods of the Image class
  195.                     // return -1 if the dimensions are not yet known. The
  196.                     // following code keeps calling getWidth() and getHeight()
  197.                     // until they return actual values.
  198.                     // NOTE: This is only executed once in this loop, since we
  199.                     //       are assuming all images are the same width and
  200.                     //       height.  However, since we do not want to duplicate
  201.                     //       the above image load code, the code resides in the
  202.                     //       loop.
  203.                     //----------------------------------------------------------
  204.                     while ((m_nImgWidth = m_Images[i-1].getWidth(null)) < 0)
  205.                         Thread.sleep(1);
  206.  
  207.                     while ((m_nImgHeight = m_Images[i-1].getHeight(null)) < 0)
  208.                         Thread.sleep(1);                        
  209.  
  210.                     // set the window to match the image size
  211.                     resize(m_nImgWidth, m_nImgHeight);                        
  212.                 }
  213.                 catch (InterruptedException e)
  214.                 {
  215.                     // TODO: Place exception-handling code here in case an
  216.                     //       InterruptedException is thrown by Thread.sleep(),
  217.                     //         meaning that another thread has interrupted this one
  218.                 }
  219.             }
  220.             m_Graphics.drawImage(m_Images[i-1], -1000, -1000, this);
  221.         }
  222.  
  223.         while (!m_fAllLoaded)
  224.         {
  225.             try
  226.             {
  227.                 Thread.sleep(10);
  228.             }
  229.             catch (InterruptedException e)
  230.             {
  231.                 // TODO: Place exception-handling code here in case an
  232.                 //       InterruptedException is thrown by Thread.sleep(),
  233.                 //         meaning that another thread has interrupted this one
  234.             }
  235.         }
  236.         
  237.         repaint();
  238.  
  239.         int nDelay = 1000 / m_fps;
  240.  
  241.         while (true)
  242.         {
  243.             try
  244.             {
  245.                 displayImage(m_Graphics);
  246.                 m_nCurrImage++;
  247.                 if (m_nCurrImage == m_numImages)
  248.                     m_nCurrImage = 0;
  249.  
  250.                 // Sleep the proper amount of time to match the 
  251.                 // frame rate
  252.                 Thread.sleep(nDelay);
  253.             }
  254.             catch (InterruptedException e)
  255.             {
  256.                 // TODO: Place exception-handling code here in case an
  257.                 //       InterruptedException is thrown by Thread.sleep(),
  258.                 //         meaning that another thread has interrupted this one
  259.                 stop();
  260.             }
  261.         }
  262.     }
  263.  
  264.  
  265.  
  266.     // TODO: Place additional applet code here
  267.  
  268. }
  269.