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

  1. /*
  2.  * @(#)BounceItem.java    1.17 95/03/17 Jonathan Payne
  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.util.Hashtable;
  21. import java.io.*;
  22. import awt.*;
  23. import net.www.html.*;
  24. import browser.*;
  25. import browser.audio.*;
  26.  
  27. class BounceImage implements Runnable {
  28.     static Image  bounceimages[] = new Image[8];
  29.     static int maxWidth;
  30.     static int maxHeight;
  31.     static float inelasticity = .96;
  32.     static float Ax = 0.0;
  33.     static float Ay = 0.0002;
  34.     static float Ar = 0.9;
  35.  
  36.     public float x = 0;
  37.     public float y = 0;
  38.     public int width;
  39.     public int height;
  40.     public float Vx = 0.1;
  41.     public float Vy = 0.05;
  42.     public int index;
  43.     public float Vr = 0.005 + (float)Math.random() * 0.001;
  44.     public float findex = 0;
  45.  
  46.     BounceItem    parent;
  47.     static boolean  imagesReadIn = false;
  48.  
  49.     public void play(int n) {
  50.     parent.play(parent.sounds[n]);
  51.     }
  52.  
  53.     private void initializeImage(int which) {
  54.     URL url = new URL(parent.appletURL, "images/jon/T" + (which + 1) + ".gif");
  55.     Image img = bounceimages[which] = parent.getImage(url);
  56.  
  57.     if (img.width > maxWidth) {
  58.         maxWidth = img.width;
  59.     }
  60.     if (img.height > maxHeight) {
  61.         maxHeight = img.height;
  62.     }
  63.     }
  64.  
  65.     public void run() {
  66.     Thread.currentThread().setPriority(Thread.MIN_PRIORITY + 1);
  67.     int i;
  68.  
  69.     for (i = 1; i < bounceimages.length; i++) {
  70.         initializeImage(i);
  71.     }
  72.     }
  73.  
  74.     public BounceImage(BounceItem parent) {
  75.     this.parent = parent;
  76.     if (!imagesReadIn) {
  77.         imagesReadIn = true;
  78.         initializeImage(0);
  79.         (new Thread(this)).start();
  80.     }
  81.     this.parent = parent;
  82.     resize(bounceimages[0].width + 2, bounceimages[0].height + 2);
  83.     }
  84.  
  85.     public void move(float x1, float y1) {
  86.     x = x1;
  87.     y = y1;
  88.     }
  89.  
  90.     public void resize(int w, int h) {
  91.     width = w;
  92.     height = h;
  93.     }
  94.  
  95.     public void paint(Graphics g) {
  96.     int i = index;
  97.  
  98.     if (bounceimages[i] == null) {
  99.         i = 0;
  100.     }
  101.     g.drawImage(bounceimages[i], (int)x, (int)y);
  102.     }
  103.  
  104.     public void step(long deltaT) {
  105.     boolean    collision_x = false;
  106.     boolean    collision_y = false;
  107.  
  108.     float jitter = (float)Math.random() * .01 - .005;
  109.  
  110.     if (width < maxWidth || height < maxHeight) {
  111.         resize(maxWidth, maxHeight);
  112.     }
  113.     x += Vx * deltaT + (Ax / 2.0) * deltaT * deltaT;
  114.     y += Vy * deltaT + (Ay / 2.0) * deltaT * deltaT;
  115.     if (x <= 0.0) {
  116.         x = 0.0;
  117.         Vx = -Vx * inelasticity + jitter;
  118.         collision_x = true;
  119.         play((int)(Math.random() * 3));
  120.     }
  121.     if (x + width >= parent.width) {
  122.         x = parent.width - width;
  123.         Vx = -Vx * inelasticity + jitter;
  124.         collision_x = true;
  125.         play((int)(Math.random() * 3));
  126.     }
  127.     if (y <= 0) {
  128.         y = 0;
  129.         Vy = -Vy * inelasticity + jitter;
  130.         collision_y = true;
  131.         play((int)(Math.random() * 3));
  132.     }
  133.     if (y + height >= parent.height) {
  134.         y = parent.height - height;
  135.         Vx *= inelasticity;
  136.         Vy = -Vy * inelasticity + jitter;
  137.         collision_y = true;
  138.     }
  139.     move(x, y);
  140.     Vy = Vy + Ay * deltaT;
  141.     Vx = Vx + Ax * deltaT;
  142.  
  143.     findex += Vr * deltaT;
  144.     if (collision_x || collision_y) {
  145.         Vr *= Ar;
  146.     }
  147.  
  148.     while (findex <= 0.0) {
  149.         findex += bounceimages.length;
  150.     }
  151.     index = ((int) findex) % bounceimages.length;
  152.  
  153.     }
  154. }
  155.  
  156. class BounceItem extends Applet implements Runnable {
  157.     boolean images_initialized = false;
  158.     BounceImage images[];
  159.  
  160.     boolean time_to_die;
  161.     InputStream music;
  162.     AudioData sounds[];
  163.  
  164.     void makeImages(int nimages) {
  165.     BounceImage    img;
  166.     int i;
  167.  
  168.     images = new BounceImage[nimages];
  169.     for (i = 0; i < nimages; i++) {
  170.         images[i] = img = new BounceImage(this);
  171.  
  172.         img.move(1 + img.width * .8 * (i % 3) + i / 3 * .3 * img.width,
  173.              img.height * .3 + (i % 3) * .3 * img.height);
  174.     }
  175.  
  176.     sounds = new AudioData[4];
  177.     sounds[0] = getAudioData("audio/bubble1.au");
  178.     sounds[1] = getAudioData("audio/bong.au");
  179.     sounds[2] = getAudioData("audio/gong.au");
  180.     sounds[3] = getAudioData("audio/train.au");
  181.     music = getContinuousAudioStream(new URL(appletURL, "audio/spacemusic.au"));
  182.     }
  183.  
  184.     public void run() {
  185.     long lasttime;
  186.  
  187.     try {
  188.         if (images == null) {
  189.         System.out.println("Making images ...");
  190.         makeImages(4);
  191.         }
  192.  
  193.         startPlaying(music);
  194.         Thread.sleep(1000);
  195.         lasttime = System.nowMillis();
  196.         while (!time_to_die) {
  197.         int i;
  198.         long now = System.nowMillis();
  199.         long deltaT = now - lasttime;
  200.         boolean active = false;
  201.  
  202.         for (i = 0; i < images.length; i++) {
  203.             BounceImage        img = images[i];
  204.  
  205.             img.step(deltaT);
  206.             if (img.Vy > .05 || -img.Vy > .05 || img.y + BounceImage.maxHeight < height - 10) {
  207.             active = true;
  208.             }
  209.         }
  210.         if (!active && images.length != 0) {
  211.             for (i = 0; i < images.length; i++) {
  212.             BounceImage img = images[i];
  213.  
  214.             img.Vx = (float)Math.random() / 4. - 0.125;
  215.             img.Vy = -(float)Math.random() / 4. - 0.2;
  216.             img.Vr = 0.05 - (float)Math.random() * 0.1;
  217.             }
  218.             play(sounds[3]);
  219.         }
  220.         repaint();
  221.         lasttime = now;
  222.         Thread.sleep(100);
  223.         }
  224.     } finally {
  225.         stopPlaying(music);
  226.     }
  227.         
  228.     }
  229.  
  230.     public void init() {
  231.     if ((width <= 100) || (height <= 100)) {
  232.         resize(500, 300);
  233.     }
  234.     }
  235.  
  236.     public void start() {
  237.     time_to_die = false;
  238.     (new Thread(this)).start();
  239.     }
  240.  
  241.     public void stop() {
  242.     time_to_die = true;
  243.     stopPlaying(music);
  244.     }
  245.  
  246.     public void paint(Graphics g) {
  247.     g.setForeground(Color.gray);
  248.     g.drawRect(0, 0, width - 1, height - 1);
  249.     if (images != null) {
  250.         for (int i = 0; i < images.length; i++) {
  251.         images[i].paint(g);
  252.         }
  253.     }
  254.     }
  255. }
  256.