home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-07-02 | 7.3 KB | 269 lines |
- // Animation - this is a simple animation routine that
- // replays the sequence of frames specified
- // in the HTML page
- import java.applet.*;
- import java.awt.*;
-
- public class Animation extends Applet implements Runnable
- {
- Thread m_Animation = null;
-
- private Graphics m_Graphics;
- private Image m_Images[];
- private int m_nCurrImage;
- private int m_nImgWidth = 0;
- private int m_nImgHeight = 0;
- private boolean m_fAllLoaded = false;
-
- // rather than hard-code the number of images,
- // get this information from the HTML page
- // private final int NUM_IMAGES = 18;
-
- 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";
-
- public Animation()
- {
- // TODO: Add constructor code here
- }
-
- public String getAppletInfo()
- {
- return "Name: Animation\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 the image 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
- }
-
- public void destroy()
- {
- // TODO: Place applet cleanup code here
- }
-
- public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h)
- {
- if (m_fAllLoaded)
- return false;
-
- if ((flags & ALLBITS) == 0)
- return true;
-
- if (++m_nCurrImage == m_numImages)
- {
- m_nCurrImage = 0;
- m_fAllLoaded = true;
- }
-
- return false;
- }
-
- private void displayImage(Graphics g)
- {
- if (!m_fAllLoaded)
- return;
-
- // since I have resized the applet window to match
- // the size of the image, there is no need to center
- // the image
- /*
- g.drawImage(m_Images[m_nCurrImage],
- (size().width - m_nImgWidth) / 2,
- (size().height - m_nImgHeight) / 2, null);
- */
- g.drawImage(m_Images[m_nCurrImage], 0, 0, null);
-
- // the following puts the frame number up in the
- // upper left hand corner - if you don't like it,
- // just comment out the following two lines
- g.setColor(Color.white);
- g.drawString("Image #" + m_nCurrImage, 10, 20);
- }
-
- public void paint(Graphics g)
- {
- // ANIMATION SUPPORT:
- // The following code displays a status message until all the
- // images are loaded. Then it calls displayImage to display the current
- // image.
- //----------------------------------------------------------------------
- if (m_fAllLoaded)
- {
- Rectangle r = g.getClipRect();
-
- g.clearRect(r.x, r.y, r.width, r.height);
- displayImage(g);
- }
- else
- g.drawString("Loading images...", 10, 20);
-
- // TODO: Place additional applet Paint code here
- }
-
- public void start()
- {
- if (m_Animation == null)
- {
- m_Animation = new Thread(this);
- m_Animation.start();
- }
- // TODO: Place additional applet start code here
- }
-
- public void stop()
- {
- if (m_Animation != null)
- {
- m_Animation.stop();
- m_Animation = null;
- }
-
- // TODO: Place additional applet stop code here
- }
-
- public void run()
- {
- repaint();
-
- m_Graphics = getGraphics();
- m_nCurrImage = 0;
- m_Images = new Image[m_numImages];
-
- String strImage;
-
- // For each image in the animation, this method first constructs a
- // string containing the path to the image file; then it begins loading
- // the image into the m_Images array. Note that the call to getImage
- // will return before the image is completely loaded.
- //----------------------------------------------------------------------
- for (int i = 1; i <= m_numImages; i++)
- {
- // strImage = "images/img00" +
- // ((i < 10) ? "0" : "") + i + ".gif";
- strImage = m_imageFile +
- ((i < 10) ? "0" : "") + i + ".gif";
- m_Images[i-1] = getImage(getDocumentBase(), strImage);
- if (m_nImgWidth == 0)
- {
- try
- {
- // The getWidth() and getHeight() methods of the Image class
- // return -1 if the dimensions are not yet known. The
- // following code keeps calling getWidth() and getHeight()
- // until they return actual values.
- // NOTE: This is only executed once in this loop, since we
- // are assuming all images are the same width and
- // height. However, since we do not want to duplicate
- // the above image load code, the code resides in the
- // loop.
- //----------------------------------------------------------
- while ((m_nImgWidth = m_Images[i-1].getWidth(null)) < 0)
- Thread.sleep(1);
-
- while ((m_nImgHeight = m_Images[i-1].getHeight(null)) < 0)
- Thread.sleep(1);
-
- // set the window to match the image size
- resize(m_nImgWidth, m_nImgHeight);
- }
- catch (InterruptedException e)
- {
- // TODO: Place exception-handling code here in case an
- // InterruptedException is thrown by Thread.sleep(),
- // meaning that another thread has interrupted this one
- }
- }
- m_Graphics.drawImage(m_Images[i-1], -1000, -1000, this);
- }
-
- while (!m_fAllLoaded)
- {
- try
- {
- Thread.sleep(10);
- }
- catch (InterruptedException e)
- {
- // TODO: Place exception-handling code here in case an
- // InterruptedException is thrown by Thread.sleep(),
- // meaning that another thread has interrupted this one
- }
- }
-
- repaint();
-
- int nDelay = 1000 / m_fps;
-
- while (true)
- {
- try
- {
- displayImage(m_Graphics);
- m_nCurrImage++;
- if (m_nCurrImage == m_numImages)
- m_nCurrImage = 0;
-
- // Sleep the proper amount of time to match the
- // frame rate
- Thread.sleep(nDelay);
- }
- catch (InterruptedException e)
- {
- // TODO: Place exception-handling code here in case an
- // InterruptedException is thrown by Thread.sleep(),
- // meaning that another thread has interrupted this one
- stop();
- }
- }
- }
-
-
-
- // TODO: Place additional applet code here
-
- }
-