home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / pc / java / un2maiq4 / pjjava / src / pj / awt / gifcanvas.java < prev    next >
Encoding:
Java Source  |  1996-08-14  |  3.8 KB  |  151 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.     Written by the Personal Journal developers of Dow Jones & Company, Inc.
  4.  
  5.     Dow Jones makes no representations or warranties about 
  6.     the suitability of this software, either express or 
  7.     implied, including but not limited to the implied warranties 
  8.     of merchantability, fitness for a particular purpose, 
  9.     or non-infringement.  Dow Jones will not be liable for 
  10.     any damages suffered by a user as a result of using, 
  11.     modifying or distributing this software or its derivatives.
  12.  
  13.  
  14.     @(#)GifCanvas.java   0.00 17-Jan-96
  15.  
  16.         A canvas that displays a GIF image.
  17.  
  18.     Authors:
  19.  
  20.         rphall      Rick Hall
  21.                 jlee        James Lee
  22.         Ted S.      Ted Skolnick
  23.  
  24.     Version Ident:
  25.  
  26.         $Header$
  27.  
  28.     History:
  29.  
  30.         17-Jan-96    rphall      Initial Creation
  31.         21-Jan-96    jlee        Added resize
  32.         24-Jan-96    jlee        Deleted resize, added dynamic resizing functionality.
  33.         27-Mar-96    Ted S.      Allow for changing of image.
  34. ---------------------------------------------------------------------------*/
  35.  
  36. package pj.awt;
  37.  
  38. import java.awt.Canvas;
  39. import java.awt.Graphics;
  40. import java.awt.Image;
  41. import java.awt.Rectangle;
  42.  
  43. /**
  44.  * A canvas that displays a GIF image.
  45.  *
  46.  * @version    0.00 17-Jan-96
  47.  * @author     rphall, jlee
  48. */
  49. public class GifCanvas extends Canvas
  50.     {
  51.  
  52.  
  53.  
  54.     // --- Instance variables
  55.  
  56.     /** The specification for this instance */
  57.  
  58.     private Image image;
  59.     private Rectangle repaintrect           = new Rectangle();
  60.     private final static long UPDATERATE    = 100;
  61.     private boolean fullrepaint             = false;
  62.  
  63.  
  64.     // --- Public constructor
  65.  
  66.     /**
  67.      * Construct a GIF canvas
  68.      * @param i A GIF image.
  69.     */
  70.     public GifCanvas(Image i)
  71.         {
  72.         image = i;
  73.         checkSize();
  74.         }
  75.  
  76.  
  77.  
  78.     /**
  79.      * Construct a GIF canvas before image is known, only to be used by derived class.
  80.     */
  81.     protected GifCanvas()
  82.         {
  83.         }
  84.  
  85.  
  86.     /**
  87.      * Change the image in use.
  88.      * @param i A GIF image.
  89.     */
  90.     public void setImage( Image i )
  91.         {
  92.         image = i;
  93.         checkSize();
  94.         }
  95.  
  96.  
  97.     // --- Public operations
  98.  
  99.     /**
  100.      * Handle updates from images being loaded.
  101.          *
  102.          * @return true if image needs more update, false otherwise.
  103.      */
  104.     public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height)
  105.         {
  106.         if ((infoflags & (WIDTH | HEIGHT)) != 0)
  107.             {
  108.             checkSize();
  109.             }
  110.         if ((infoflags & (SOMEBITS | FRAMEBITS | ALLBITS)) != 0)
  111.             {
  112.             repaint( ((infoflags & (FRAMEBITS | ALLBITS)) != 0) ? 0 : UPDATERATE, x, y, width, height );
  113.             }
  114.         return (infoflags & (ALLBITS | ERROR)) == 0;
  115.         }
  116.  
  117.  
  118.     /**
  119.      * Paint the canvas with the GIF image.  If the image
  120.      * is null, paint the canvas with the default background color.
  121.     */
  122.     public void paint(Graphics g)
  123.         {
  124.         if (image == null)
  125.             super.paint(g);
  126.         else
  127.             g.drawImage(image,0,0,this);
  128.         }
  129.  
  130.     // --- Private operations
  131.  
  132.     /**
  133.      * Check the size of this canvas while the image is being loaded.
  134.      */
  135.     private synchronized void checkSize()
  136.         {
  137.         int w = image.getWidth(this);
  138.         int h = image.getHeight(this);
  139.         if (w > 0 && h > 0)
  140.             {
  141.             resize(w, h);
  142.             repaintrect.x = repaintrect.y = 0;
  143.             repaintrect.width = w;
  144.             repaintrect.height = h;
  145.             fullrepaint = true;
  146.             repaint();
  147.             }
  148.         }
  149.  
  150.     } // GifCanvas
  151.