home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-09-09 | 14.8 KB | 401 lines |
- //*****************************************************************
- // Applet : SpinningRedTorus.java
- //
- // Author : Rob McGregor
- //
- // Comments : Adapted from the Visual J++ Applet Wizard default
- // animation applet
- //*****************************************************************
-
- import java.applet.*;
- import java.awt.*;
-
- //=================================================================
- // Main Class for applet SpinningRedTorus
- //=================================================================
-
- public class SpinningRedTorus extends Applet implements Runnable
- {
- //--------------------------------------------------------------
- // THREAD SUPPORT:
- //--------------------------------------------------------------
- // m_SpinningRedTorus Thread object for the applet
- //--------------------------------------------------------------
- Thread m_SpinningRedTorus = null;
-
- //--------------------------------------------------------------
- // ANIMATION SUPPORT:
- //--------------------------------------------------------------
- // m_Graphics stores the applet's Graphics context
- // m_Images[] the array of Image objects for the animation
- // m_nCurrImage the index of the next image to be displayed
- // m_ImgWidth width of each image
- // m_ImgHeight height of each image
- // m_fAllLoaded indicates whether all images have been loaded
- // IMAGE_LAST number of images used in the animation
- //--------------------------------------------------------------
- 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;
-
- // Constants
- private final int NUM_IMAGES = 12;
- private final int FORWARD = 1;
- private final int BACKWARD = 0;
-
- //--------------------------------------------------------------
- // PARAMETER SUPPORT:
- //--------------------------------------------------------------
- // Parameters allow an HTML author to pass information to
- // the applet; the HTML author specifies them using the <PARAM>
- // tag within the <APPLET> tag. The following variables are
- // used to store the values of the parameters.
- //--------------------------------------------------------------
-
- // Members for applet parameters
- private int m_Interval = 100;
- private int m_Direction = 0;
-
- // 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_Interval = "Interval";
- private final String PARAM_Direction = "Direction";
-
- //==============================================================
- // SpinningRedTorus Class Constructor
- //==============================================================
-
- public SpinningRedTorus()
- {
- // Add constructor code here
- }
-
- //==============================================================
- // public String getAppletInfo()
- //--------------------------------------------------------------
- // Returns a string describing the applet's author, copyright
- // date, or miscellaneous information
- //==============================================================
-
- public String getAppletInfo()
- {
- return "Name: SpinningRedTorus\r\n" +
- "Author: Rob McGregor\r\n" +
- "Created with Microsoft Visual J++ Version 1.0";
- }
-
- //==============================================================
- // public String[][] getParameterInfo()
- //--------------------------------------------------------------
- // The getParameterInfo() method returns an array of strings
- // describing the parameters understood by this applet.
- //
- // SpinningRedTorus Parameter Information:
- // { "Name", "Type", "Description" },
- //==============================================================
-
- public String[][] getParameterInfo()
- {
- String[][] info =
- {
- { PARAM_Interval, "int", "Interval between frames" },
- { PARAM_Direction, "int", "Forward or backward (1 or 0)" },
- };
- return info;
- }
-
- //==============================================================
- // public void init()
- //--------------------------------------------------------------
- // Called by the AWT when an applet is first loaded or reloaded
- // Override this method to perform whatever initialization your
- // applet needs, such as initializing data structures, loading
- // images or fonts, creating frame windows, setting the layout
- // manager, or adding UI components.
- //==============================================================
-
- public void init()
- {
- // The following code retrieves the value of each parameter
- // specified with the <PARAM> tag and stores it in a member
- // variable.
- //-----------------------------------------------------------
- String param;
-
- // Interval: Interval between frames
- //-----------------------------------------------------------
- param = getParameter(PARAM_Interval);
- if (param != null)
- m_Interval = Integer.parseInt(param);
-
- // Direction: Forward or backward (1 or 0)
- //-----------------------------------------------------------
- param = getParameter(PARAM_Direction);
- if (param != null)
- m_Direction = Integer.parseInt(param);
-
- resize(160, 120);
- }
-
- //==============================================================
- // public void destroy()
- //--------------------------------------------------------------
- // Called when when the applet is terminating, place additional
- // applet clean up code here.
- //==============================================================
-
- public void destroy()
- {
- // Place applet cleanup code here
- }
-
- //==============================================================
- // public boolean imageUpdate()
- //--------------------------------------------------------------
- // The imageUpdate() method is called repeatedly by the AWT
- // while images are being constructed. The flags parameter
- // provides information about the status of images under
- // construction. This method checks whether the ALLBITS flag is
- // set, which means that an image is completely loaded. When all
- // the images are completely loaded, this method sets the
- // m_fAllLoaded variable to true so that animation can begin.
- //==============================================================
-
- public boolean imageUpdate(Image img, int flags, int x, int y,
- int w, int h)
- {
- // Nothing to do if images are all loaded
- //-----------------------------------------------------------
- if (m_fAllLoaded)
- return false;
-
- // Want all bits to be available before painting
- //-----------------------------------------------------------
- if ((flags & ALLBITS) == 0)
- return true;
-
- // All bits are available, so increment loaded count of fully
- // loaded images, starting animation if all images are loaded
- //-----------------------------------------------------------
- if (++m_nCurrImage == NUM_IMAGES)
- {
- m_nCurrImage =
- (FORWARD == m_Direction) ? 0 : NUM_IMAGES - 1;
-
- m_fAllLoaded = true;
- }
-
- return false;
- }
-
- //==============================================================
- // private void displayImage()
- //--------------------------------------------------------------
- // Draws the next image (if all images are currently loaded)
- //==============================================================
-
- private void displayImage(Graphics g)
- {
- if (!m_fAllLoaded)
- return;
-
- // Draw Image in center of applet
- //-----------------------------------------------------------
- g.drawImage(m_Images[m_nCurrImage],
- (size().width - m_nImgWidth) / 2,
- (size().height - m_nImgHeight) / 2, null);
- }
-
- //==============================================================
- // public void paint(Graphics g)
- //--------------------------------------------------------------
- // SpinningRedTorus Paint Handler
- //==============================================================
-
- public void paint(Graphics g)
- {
- // 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 ray traced images...", 10, 20);
- }
-
- //==============================================================
- // public void start()
- //--------------------------------------------------------------
- // Called when the page containing the applet first appears on
- // the screen to start execution of the applet's thread.
- //==============================================================
-
- public void start()
- {
- if (m_SpinningRedTorus == null)
- {
- m_SpinningRedTorus = new Thread(this);
- m_SpinningRedTorus.start();
- }
- }
-
- //==============================================================
- // public void stop()
- //--------------------------------------------------------------
- // Called when the page containing the applet is no longer on
- // the screen. This method stops execution of the applet's
- // thread.
- //==============================================================
-
- public void stop()
- {
- if (m_SpinningRedTorus != null)
- {
- m_SpinningRedTorus.stop();
- m_SpinningRedTorus = null;
- }
- }
-
- //==============================================================
- // public void run()
- //--------------------------------------------------------------
- // Called when the applet's thread is started. If your applet
- // performs any ongoing activities without waiting for user
- // input, the code for implementing that behavior typically goes
- // here. For example, for an applet that performs animation,
- // the run() method controls the display of images.
- //==============================================================
-
- public void run()
- {
- repaint();
-
- m_Graphics = getGraphics();
- m_nCurrImage = 0;
- m_Images = new Image[NUM_IMAGES];
-
- // Load in all the images
- //-----------------------------------------------------------
- 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 <= NUM_IMAGES; i++)
- {
- // Build path to next image
- //--------------------------------------------------------
- strImage = "./images/img00" + ((i < 10) ? "0" : "")
- + i + ".gif";
- m_Images[i-1] = getImage(getDocumentBase(), strImage);
-
- // Get width and height of one image.
- // Assuming all images are same width and height
- //--------------------------------------------------------
- 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);
- }
- catch (InterruptedException e)
- {
- // Place exception-handling code here in case an
- // InterruptedException is thrown by Thread.sleep(),
- // meaning that another thread has interrupted this
- // one
- }
- }
-
- // Force image to fully load
- //--------------------------------------------------------
- m_Graphics.drawImage(m_Images[i-1], -1000, -1000, this);
- }
-
- // Wait until all images are fully loaded
- //-----------------------------------------------------------
- while (!m_fAllLoaded)
- {
- try
- {
- Thread.sleep(10);
- }
- catch (InterruptedException e)
- {
- // Place exception-handling code here in case an
- // InterruptedException is thrown by Thread.sleep(),
- // meaning that another thread has interrupted this
- // one
- }
- }
-
- repaint();
-
- while (true)
- {
- try
- {
- // Draw next image in animation
- //-----------------------------------------------------
- displayImage(m_Graphics);
- if (m_Direction == FORWARD) // 1
- {
- m_nCurrImage++;
- if (m_nCurrImage == NUM_IMAGES)
- m_nCurrImage = 0;
- }
- if (m_Direction == BACKWARD) // 0
- {
- m_nCurrImage--;
- if (m_nCurrImage <= 0)
- m_nCurrImage = NUM_IMAGES - 1;
- }
-
- // Set the animation to desired speed
- Thread.sleep(m_Interval);
- }
- catch (InterruptedException e)
- {
- // Place exception-handling code here in case an
- // InterruptedException is thrown by Thread.sleep(),
- // meaning that another thread has interrupted this
- // one
- stop();
- }
- }
- }
- }
-