home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / constr~1 / underc~1 / jackha~1.jav < prev   
Encoding:
Text File  |  1995-10-31  |  3.7 KB  |  144 lines

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