home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / windows / java / demo / imagemap / delayedsoundarea.java < prev    next >
Encoding:
Java Source  |  1996-04-26  |  3.6 KB  |  113 lines

  1. /*
  2.  * @(#)DelayedSoundArea.java    1.5 96/04/24  
  3.  *
  4.  * Copyright (c) 1994-1996 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.awt.Graphics;
  33. import java.applet.AudioClip;
  34. import java.net.URL;
  35. import java.net.MalformedURLException;
  36. import java.util.StringTokenizer;
  37.  
  38. /**
  39.  * This ImageArea Class will play a sound each time the user enters the 
  40.  * area. It is different from SoundArea in that it accepts a delay (in 
  41.  * tenths of a second) before it plays the sound. If the mouse leaves
  42.  * the area before the time delay, the sound is not played.
  43.  *
  44.  * This allows you to have one piece of audio when the button is "hit"
  45.  * via SoundArea and another if the user stays on the button.
  46.  *
  47.  * @author     Chuck McManis
  48.  * @version     1.5, 04/24/96
  49.  */
  50. class DelayedSoundArea extends ImageMapArea {
  51.     /** The URL of the sound to be played. */
  52.     URL     sound;
  53.     AudioClip    soundData;
  54.     boolean     hasPlayed; 
  55.     int            delay;
  56.     int        countDown;
  57.  
  58.     /**
  59.      * The argument is the URL of the sound to be played.
  60.      * This method also sets this type of area to be non-terminal.
  61.      */
  62.     public void handleArg(String arg) {
  63.     Thread soundLoader;
  64.     StringTokenizer st = new StringTokenizer(arg, ", ");
  65.     
  66.     delay = Integer.parseInt(st.nextToken());
  67.     try {
  68.         sound = new URL(parent.getDocumentBase(), st.nextToken());
  69.     } catch (MalformedURLException e) {
  70.         sound = null;
  71.     }
  72.     }
  73.  
  74.     public void getMedia() {
  75.     if (sound != null) {
  76.         soundData = parent.getAudioClip(sound);
  77.     }
  78.     if (soundData == null) {
  79.         System.out.println("DelayedSoundArea: Unable to load data "+sound);
  80.     }
  81.     }
  82.  
  83.     /**
  84.      * The highlight method plays the sound in addition to the usual
  85.      * graphical highlight feedback.
  86.      */
  87.     public void enter() {
  88.     hasPlayed = false;
  89.     countDown = delay;
  90.     parent.startAnimation();
  91.     }
  92.  
  93.     /**
  94.      * This method is called every animation cycle if there are any
  95.      * active animating areas.
  96.      * @return true if this area requires further animation notifications
  97.      */
  98.     public boolean animate() {
  99.     if (entered && ! hasPlayed) {
  100.         if (countDown > 0) {
  101.         countDown--;
  102.         return true;
  103.         }
  104.         hasPlayed = true;
  105.         if (soundData != null) {
  106.             soundData.play();
  107.         }
  108.     }
  109.     return false;
  110.     }
  111. }
  112.  
  113.