home *** CD-ROM | disk | FTP | other *** search
/ Computer Life: Multimedia Mega Pac / Multimedia_Mega-Pac_Computer_Life_1996.iso / hotjava / demo / classes / imageloo.jav < prev    next >
Text File  |  1995-05-19  |  5KB  |  200 lines

  1. /*
  2.  * @(#)ImageLoopItem.java    1.22 95/03/28 James Gosling
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. import java.io.InputStream;
  21. import awt.*;
  22. import browser.*;
  23. import net.www.html.*;
  24.  
  25. /**
  26.  * ImageLoop class. This is a container for a list
  27.  * of images that can be animated.
  28.  *
  29.  * @author     James Gosling
  30.  * @version     1.22, 28 Mar 1995
  31.  */
  32. class ImageLoop {
  33.     /**
  34.      * The images.
  35.      */
  36.     Image imgs[];
  37.  
  38.     /**
  39.      * The number of images actually loaded.
  40.      */
  41.     int nimgs = 0;
  42.  
  43.     /**
  44.      * Load the images, from dir. The images are assumed to be
  45.      * named T1.gif, T2.gif...
  46.      * Once all images are loaded the applet is resized to the
  47.      * maximum width and height.
  48.      */
  49.     ImageLoop(URL context, String dir, ImageLoopItem parent) {
  50.     int maxWidth = 0;
  51.     int maxHeight = 0;
  52.  
  53.     imgs = new Image[40];
  54.     for (int i = 1; i < imgs.length; i++) {
  55.         Image im = parent.getImage(dir + "/T" + i + ".gif");
  56.  
  57.         if (im == null) {
  58.         break;
  59.         }
  60.  
  61.         imgs[nimgs++] = im;
  62.         if (im.width > maxWidth) {
  63.         maxWidth = im.width;
  64.         }
  65.         if (im.height > maxHeight) {
  66.         maxHeight = im.height;
  67.         }
  68.     }
  69.     parent.resize(maxWidth, maxHeight);
  70.     }
  71. }
  72.  
  73. /**
  74.  * A simple Item class to play an image loop.  The "img" tag parameter
  75.  * indicates what image loop to play.
  76.  *
  77.  * @author     James Gosling
  78.  * @version     1.22, 28 Mar 1995
  79.  */
  80. public
  81. class ImageLoopItem extends Applet implements Runnable {
  82.     /**
  83.      * The current loop slot.
  84.      */
  85.     int loopslot = 0;
  86.  
  87.     /**
  88.      * The directory or URL from which the images are loaded
  89.      */
  90.     String dir;
  91.  
  92.     /**
  93.      * The image loop.
  94.      */
  95.     ImageLoop loop;
  96.  
  97.     /**
  98.      * The thread animating the images.
  99.      */
  100.     Thread kicker = null;
  101.  
  102.     /**
  103.      * The length of the pause between revs.
  104.      */
  105.     int pause;
  106.  
  107.     /**
  108.      * Whether or not the thread has been paused by the user.
  109.      */
  110.     boolean threadSuspended = false;
  111.  
  112.     /**
  113.      * The offscreen image.
  114.      */
  115.     Image    im;
  116.  
  117.     /**
  118.      * The offscreen graphics context
  119.      */
  120.     Graphics    offscreen;
  121.  
  122.     /**
  123.      * Initialize the applet. Get attributes.
  124.      */
  125.     public void init() {
  126.     String at = getAttribute("img");
  127.     dir = (at != null) ? at : "doc:/demo/images/duke";
  128.     at = getAttribute("pause");
  129.     pause = (at != null) ? 0 : 3900;
  130.     }
  131.  
  132.     /**
  133.      * Run the image loop. This methods is called by class Thread.
  134.      * @see java.lang.Thread
  135.      */
  136.     public void run() {
  137.     Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  138.     loop = new ImageLoop(documentURL, dir, this);
  139.  
  140.     if (loop.nimgs > 1) {
  141.         while (width > 0 && height > 0 && kicker != null) {
  142.         if (++loopslot >= loop.nimgs) {
  143.             loopslot = 0;
  144.         }
  145.         repaint();
  146.         Thread.sleep(100 + ((loopslot == 0) ? pause : 0));
  147.         }
  148.     }
  149.     }
  150.  
  151.     /**
  152.      * Paint the current frame.
  153.      */
  154.     public void paint(Graphics g) {
  155.     update(g);
  156.     }
  157.     public void update(Graphics g) {
  158.     if ((loop != null) && (loop.imgs != null) &&
  159.         (loopslot < loop.nimgs) && (loop.imgs[loopslot] != null)) {
  160.         if (im == null) {
  161.         im = createImage(width, height);
  162.         offscreen = new Graphics(im);
  163.         offscreen.setForeground(Color.lightGray);
  164.         }
  165.         offscreen.fillRect(0, 0, width, height);
  166.         offscreen.drawImage(loop.imgs[loopslot], 0, 0);
  167.         g.drawImage(im, 0, 0);
  168.     }
  169.     }
  170.  
  171.     /**
  172.      * Start the applet by forking an animation thread.
  173.      */
  174.     public void start() {
  175.     if (kicker == null) {
  176.         kicker = new Thread(this);
  177.         kicker.start();
  178.     }
  179.     }
  180.  
  181.     /**
  182.      * Stop the applet. The thread will exit because kicker is set to null.
  183.      */
  184.     public void stop() {
  185.     kicker = null;
  186.     }
  187.  
  188.     /**
  189.      * Pause the thread when the user clicks the mouse in the applet.
  190.      */
  191.     public void mouseDown(int x, int y) {
  192.         if (threadSuspended)
  193.             kicker.resume();
  194.     else
  195.             kicker.suspend();
  196.         threadSuspended = !threadSuspended;
  197.     }
  198.  
  199. }
  200.