home *** CD-ROM | disk | FTP | other *** search
/ BUG 15 / BUGCD1998_06.ISO / aplic / jbuilder / jsamples.z / DelayedSoundArea.java < prev    next >
Encoding:
Java Source  |  1997-07-30  |  3.9 KB  |  113 lines

  1. // $Header: z:/admin/metro_examples/java/demo/ImageMap/rcs/DelayedSoundArea.java 1.1 1997/02/06 00:30:15 IPGIntel-2 Exp $ 
  2. /*
  3.  * @(#)DelayedSoundArea.java    1.6 96/12/06
  4.  *
  5.  * Copyright (c) 1994-1996 Sun Microsystems, Inc. All Rights Reserved.
  6.  *
  7.  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use,
  8.  * modify and redistribute this software in source and binary code form,
  9.  * provided that i) this copyright notice and license appear on all copies of
  10.  * the software; and ii) Licensee does not utilize the software in a manner
  11.  * which is disparaging to Sun.
  12.  *
  13.  * This software is provided "AS IS," without a warranty of any kind. ALL
  14.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
  15.  * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  16.  * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
  17.  * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
  18.  * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
  19.  * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
  20.  * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
  21.  * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
  22.  * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
  23.  * POSSIBILITY OF SUCH DAMAGES.
  24.  *
  25.  * This software is not designed or intended for use in on-line control of
  26.  * aircraft, air traffic, aircraft navigation or aircraft communications; or in
  27.  * the design, construction, operation or maintenance of any nuclear
  28.  * facility. Licensee represents and warrants that it will not use or
  29.  * redistribute the Software for such purposes.
  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.6, 12/06/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.