home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / ActiveX / JBrowser / JAnim.java next >
Encoding:
Java Source  |  2000-05-04  |  3.6 KB  |  159 lines

  1. //////////////////////////////////////////////////////////////////////////
  2. // JAnim.java
  3. // This brings up a canvas which shows an animated image.
  4. // 
  5. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  6. //////////////////////////////////////////////////////////////////////////
  7.  
  8. // afc
  9. import com.ms.ui.*;
  10. import com.ms.ui.event.*;
  11. import com.ms.fx.*;
  12.  
  13. // standard java
  14. import java.awt.*;
  15. import java.awt.image.*;
  16.  
  17. //
  18. // JAnim
  19. //
  20. public class JAnim extends UICanvas implements Runnable
  21. {
  22.     int numImages = 0;                                        // The number of images
  23.     int numSkip = 0;                                        // The number of images to skip
  24.     Image    image ;                                            // the large image
  25.     int width;                                                // the width of each image
  26.     int height;                                                // the height of each image
  27.     int currentImage = -1;                                    // the current image being displayed
  28.  
  29.     /**
  30.      * This constructor of the JAnim object requires an image object and
  31.      * and the number of images which are concatenated in this image object
  32.      *
  33.      * @param    Image    The big concatenated image
  34.      * @param    int        The number of images stacked one on top of the other
  35.      * @param    int        The number of images to skip after all images are shown and a new round begins
  36.      */
  37.     public JAnim(Image image, int numImages, int numSkip)
  38.     {
  39.         MediaTracker tracker = new MediaTracker(new Canvas() );
  40.  
  41.         tracker.addImage(image, 0);
  42.  
  43.         // wait for the image to be loaded
  44.         try 
  45.         {
  46.             tracker.waitForAll();
  47.         } 
  48.         catch (InterruptedException e) 
  49.         {
  50.         }
  51.  
  52.  
  53.         this.numImages = numImages;
  54.         this.image = image;
  55.         this.numSkip = numSkip;
  56.  
  57.         // calculate the width and height
  58.         width = image.getWidth(this);
  59.         height = image.getHeight(this) / numImages;
  60.  
  61.         currentImage = 0;
  62.     }
  63.  
  64.     /**
  65.      * This returns the preferred size for the object
  66.      * 
  67.      * @param    Dimension    The preferred dimension
  68.      */
  69.     public Dimension getPreferredSize()
  70.     {
  71.         return new Dimension(width, height);
  72.     }
  73.  
  74.  
  75.     /**
  76.      * This returns the minimum size for the object
  77.      * 
  78.      * @param    Dimension    The preferred dimension
  79.      */
  80.     public Dimension getMinimumSize()
  81.     {
  82.         return new Dimension(width, height);
  83.     }
  84.  
  85.  
  86.     /**
  87.      * This method paints the JAnim object -- actually it just calls the update method
  88.      *
  89.      * @param    FxGraphics    The grahics context to paint with.
  90.      */
  91.     public void paint(FxGraphics g)
  92.     {
  93.         update(g);
  94.     }
  95.  
  96.     /**
  97.      * This method will draw the current image in the JAnim object
  98.      *
  99.      * @param    FxGraphics    The graphics context to paint with
  100.      */
  101.     public void update(FxGraphics g)
  102.     {
  103.         // we have to calculate the source coordinates of the current image in the tall image
  104.         g.drawImage(image, 0, 0, width-1, height-1, 0, currentImage*height, width-1, currentImage*height+height-1, this); 
  105.     }
  106.  
  107.     /**
  108.      * This is the run method of the JAnim object it repeatedly displays the list of images
  109.      * in the image object. It sleeps for half a second between images
  110.      */
  111.     public void run()
  112.     {
  113.         // first time around all the images must be shown but in subsequent
  114.         // runs pick up after skipping the initial few.
  115.         for(int i=0; i<numImages; i++)
  116.         {
  117.             currentImage = i;
  118.             repaint();
  119.             try
  120.             {
  121.                 Thread.sleep(120);
  122.             }
  123.             catch(InterruptedException e)
  124.             {
  125.                 e.printStackTrace();
  126.             }
  127.         }
  128.  
  129.         while(true)
  130.         {
  131.             for(int i=numSkip; i<numImages; i++)
  132.             {
  133.                 currentImage = i;
  134.                 repaint();
  135.  
  136.                 try
  137.                 {
  138.                     Thread.sleep(120);
  139.                 }
  140.                 catch(InterruptedException e)
  141.                 {
  142.                     e.printStackTrace();
  143.                 }
  144.  
  145.             }
  146.         }
  147.  
  148.     }
  149.  
  150.     /**
  151.      * This method resets the JAnim object to display the first image in the list
  152.      */
  153.     public void reset()
  154.     {
  155.         currentImage = 0;
  156.         repaint();
  157.     }
  158.  
  159. }