home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 December / PCWKCD1296.iso / vjplusb / msdev / samples / sun / tumblingduke / tumbleitem.java < prev    next >
Text File  |  1996-07-10  |  5KB  |  171 lines

  1. /*
  2.  * @(#)Tumble.java    
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  8.  * without fee is hereby granted. 
  9.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  10.  * for further important copyright and trademark information and to
  11.  * http://java.sun.com/licensing.html for further important licensing
  12.  * information for the Java (tm) Technology.
  13.  * 
  14.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  15.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  16.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  18.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  19.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  20.  * 
  21.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  22.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  23.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  24.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  25.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  26.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  27.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  28.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  29.  * HIGH RISK ACTIVITIES.
  30.  */
  31.  
  32. import java.io.InputStream;
  33. import java.applet.Applet;
  34. import java.awt.*;
  35. import java.net.*;
  36.  
  37. /**
  38.  * A simple Item class to play an image loop.  The "img" tag parameter
  39.  * indicates what image loop to play.
  40.  *
  41.  * @author     James Gosling
  42.  * @version     1.17, 31 Jan 1995
  43.  */
  44. public class tumbleitem extends Applet implements Runnable {
  45.     /**
  46.      * The current loop slot.
  47.      */
  48.     int loopslot = 0;
  49.  
  50.     /**
  51.      * The directory or URL from which the images are loaded
  52.      */
  53.     String dir;
  54.  
  55.     /**
  56.      * The thread animating the images.
  57.      */
  58.     Thread kicker = null;
  59.  
  60.     /**
  61.      * The length of the pause between revs.
  62.      */
  63.     int pause;
  64.  
  65.     int offset;
  66.     int off;
  67.     int speed;
  68.     int nimgs;
  69.  
  70.     /**
  71.      * The images.
  72.      */
  73.     Image imgs[];
  74.     int maxWidth;
  75.  
  76.     /**
  77.      * Initialize the applet. Get attributes.
  78.      */
  79.     public void init() {
  80.     String at = getParameter("img");
  81.     dir = (at != null) ? at : "images/tumble";
  82.     at = getParameter("pause");
  83.     pause = (at != null) ? Integer.valueOf(at).intValue() : 3900;
  84.     at = getParameter("offset");
  85.     offset = (at != null) ? Integer.valueOf(at).intValue() : 0;
  86.     at = getParameter("speed");
  87.     speed = (at != null) ? (1000 / Integer.valueOf(at).intValue()) : 100;
  88.     at = getParameter("nimgs");
  89.     nimgs = (at != null) ? Integer.valueOf(at).intValue() : 16;
  90.     at = getParameter("maxwidth");
  91.     maxWidth = (at != null) ? Integer.valueOf(at).intValue() : 0;
  92.     }
  93.  
  94.     /**
  95.      * Run the image loop. This methods is called by class Thread.
  96.      * @see java.lang.Thread
  97.      */
  98.     public void run() {
  99.     Thread.currentThread().setPriority(Thread.NORM_PRIORITY-1);
  100.     imgs = new Image[nimgs];
  101.     for (int i = 1; i < nimgs; i++) {
  102.         imgs[i] = getImage(getDocumentBase(), dir + "/T" + i + ".gif");
  103.     }
  104.  
  105.     Dimension d = size();
  106.     if (nimgs > 1) {
  107.         if (offset < 0) {
  108.         off = d.width - maxWidth;
  109.         }
  110.         while (kicker != null) {
  111.         //System.out.println("frame = " +  loopslot);
  112.         if (++loopslot >= nimgs) {
  113.             loopslot = 0;
  114.             off += offset;
  115.             if (off < 0) {
  116.             off = d.width - maxWidth;
  117.             } else if (off + maxWidth > d.width) {
  118.             off = 0;
  119.             }
  120.         }
  121.         repaint();
  122.         try {
  123.             Thread.sleep(speed + ((loopslot == nimgs - 1) ? pause : 0));
  124.         } catch (InterruptedException e) {
  125.             break;
  126.         }
  127.         }
  128.     }
  129.     }
  130.  
  131.     public boolean imageUpdate(Image img, int flags,
  132.                    int x, int y, int w, int h) {
  133.     if ((flags & (SOMEBITS|FRAMEBITS|ALLBITS)) != 0) {
  134.         if ((imgs != null) && (loopslot < nimgs) && (imgs[loopslot] == img)) {
  135.         repaint(100);
  136.         }
  137.     }
  138.     return (flags & (ALLBITS|ERROR)) == 0;
  139.     }
  140.  
  141.     /**
  142.      * Paint the current frame.
  143.      */
  144.     public void paint(Graphics g) {
  145.     //System.out.println("paint");
  146.     if ((imgs != null) && (loopslot < nimgs) && (imgs[loopslot] != null)) {
  147.         g.drawImage(imgs[loopslot], off, 0, this);
  148.     }
  149.     }
  150.  
  151.     /**
  152.      * Start the applet by forking an animation thread.
  153.      */
  154.     public void start() {
  155.     if (kicker == null) {
  156.         kicker = new Thread(this);
  157.         kicker.start();
  158.     }
  159.     }
  160.  
  161.     /**
  162.      * Stop the applet. The thread will exit because kicker is set to null.
  163.      */
  164.     public void stop() {
  165.     if (kicker != null) {
  166.         kicker.stop();
  167.         kicker = null;
  168.     }
  169.     }
  170. }
  171.