home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 December / PCWKCD1296.iso / vjplusb / msdev / samples / sun / underconstruction / jackhammerduke.java < prev    next >
Encoding:
Java Source  |  1996-07-10  |  4.4 KB  |  156 lines

  1. /*
  2.  * %W% %E% Bob Weisblatt
  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.awt.*;
  34. import java.net.*;
  35.  
  36. /**
  37.  * A simple java.applet.Applet class to play an image loop.  The "img" tag parameter
  38.  * indicates what image loop to play.
  39.  *
  40.  * @author     Bob Weisblatt (major thievery from jag, maybe wholesale is a better word.)
  41.  * @version     1.07f, 19 Jan 1995
  42.  */
  43.  
  44. public class jackhammerduke extends java.applet.Applet implements Runnable {
  45.     /**
  46.      * The current loop slot.
  47.      */
  48.     int seqslot = 0;
  49.  
  50.     /**
  51.      * The number of images
  52.      */
  53.     int nimgs = 4;
  54.  
  55.     /**
  56.      * A strip of 4 images.
  57.      */
  58.     Image imgs;
  59.  
  60.     /**
  61.      * The directory or URL from which the images are loaded
  62.      */
  63.     String dir;
  64.  
  65.     /**
  66.      * The thread animating the images.
  67.      */
  68.     Thread kicker = null;
  69.  
  70.     /**
  71.      * The length of the pause between revs.lkj 
  72.      */
  73.     int pause;
  74.  
  75.     /**
  76.      * The current x position.
  77.      */
  78.     double x;
  79.  
  80.     int imgsWidth = 328;
  81.     int imgsHeight = 90;
  82.  
  83.     /**
  84.      * Run the image loop. This method is called by class Thread.
  85.      * @see oak.lang.Thread
  86.      *
  87.      * getDocumentBase() is the URL of the document in which the applet is
  88.      * embedded. It shouldn't be changed.
  89.      *
  90.      * dir is in net.www.html.Tag but is undocumented.
  91.      */
  92.     public void run() {
  93.     Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  94.  
  95.     imgs = getImage(getCodeBase(), "images/jack.gif");
  96.  
  97.     if (imgs != null) {
  98.         x = (size().width - imgsWidth/nimgs) / 2;
  99.         while (size().width > 0 && size().height > 0 && kicker != null) {
  100.         if (seqslot == 0) {
  101.             play(getCodeBase(), "audio/jackhammer-short.au");
  102.         }
  103.  
  104.         repaint();
  105.         try {Thread.sleep((seqslot == sequence.length -1) ? 500 : 100);} catch (InterruptedException e){}
  106.         seqslot = (seqslot + 1) % sequence.length;
  107.         }
  108.     }
  109.  
  110.     }
  111.  
  112.     private int sequence[] = { 2, 1, 0, 1, 0, 1, 0, 1, 2, 1, 0, 2, 0, 1, 0, 2, 3};
  113.  
  114.     /**
  115.      * Paint the current frame.
  116.      */
  117.     boolean erase;
  118.  
  119.     public void update(Graphics g) {
  120.     if (erase || (sequence[seqslot] == 3)) {
  121.         g.setColor(Color.lightGray);
  122.         g.fillRect(0, 0, size().width, size().height);
  123.         erase = false;
  124.     }
  125.     paint(g);
  126.     }
  127.     public void paint(Graphics g) {
  128.     int loopslot = sequence[seqslot];
  129.     if ((imgs != null) && (loopslot < nimgs)) {
  130.         int w = imgsWidth / nimgs;
  131.         x = Math.max(0, Math.min(size().width - w, x + Math.random() * 4 - 2));
  132.         
  133.         g.clipRect((int)x, 0, w, imgsHeight);
  134.         g.drawImage(imgs, (int)x - loopslot * w, 0, this);
  135.         erase = (loopslot == 3);
  136.     }
  137.     }
  138.  
  139.     /**
  140.      * Start the applet by forking an animation thread.
  141.      */
  142.     public void start() {
  143.     if (kicker == null) {
  144.         kicker = new Thread(this);
  145.         kicker.start();
  146.     }
  147.     }
  148.  
  149.     /**
  150.      * Stop the applet. The thread will exit because kicker is set to null.
  151.      */
  152.     public void stop() {
  153.     kicker = null;
  154.     }
  155. }
  156.