home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / applets / ImageMap / SoundArea.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  4.1 KB  |  131 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)SoundArea.java    1.12 02/06/13
  38.  */
  39.  
  40. import java.awt.Graphics;
  41. import java.applet.AudioClip;
  42. import java.net.URL;
  43. import java.net.MalformedURLException;
  44.  
  45. /**
  46.  * An audio feedback ImageArea class.
  47.  * This class extends the basic ImageArea Class to play a sound when
  48.  * the user enters the area.
  49.  *
  50.  * @author     Jim Graham
  51.  * @author     Chuck McManis
  52.  * @version     1.12, 06/13/02
  53.  */
  54. class SoundArea extends ImageMapArea {
  55.     /** The URL of the sound to be played. */
  56.     URL sound;
  57.     AudioClip soundData = null;
  58.     boolean hasPlayed; 
  59.     boolean isReady = false;
  60.     long    lastExit = 0;
  61.     final static int HYSTERESIS = 1500;
  62.  
  63.     /**
  64.      * The argument is the URL of the sound to be played.
  65.      */
  66.     public void handleArg(String arg) {
  67.     try {
  68.         sound = new URL(parent.getDocumentBase(), arg);
  69.     } catch (MalformedURLException e) {
  70.         sound = null;
  71.     }
  72.     hasPlayed = false;
  73.     }
  74.  
  75.     /**
  76.      * The applet thread calls the getMedia() method when the applet
  77.      * is started.
  78.      */
  79.     public void getMedia() {
  80.     if (sound != null && soundData == null) {
  81.         soundData = parent.getAudioClip(sound);
  82.     }
  83.     if (soundData == null) {
  84.         System.out.println("SoundArea: Unable to load data "+sound);
  85.     }
  86.     isReady = true;
  87.     }
  88.  
  89.     /**
  90.      * The enter method is called when the mouse enters the area.
  91.      * The sound is played if the mouse has been outside of the
  92.      * area for more then the delay indicated by HYSTERESIS.
  93.      */
  94.     public void enter() {
  95.     // is the sound sample loaded?
  96.     if (! isReady) {
  97.         parent.showStatus("Loading media file...");
  98.         return;
  99.     }
  100.  
  101.     /*
  102.       * So we entered the selection region, play the sound if
  103.      * we need to. Track the mouse entering and exiting the
  104.      * the selection box. If it doesn't stay out for more than
  105.      * "HYSTERESIS" millis, then don't re-play the sound.
  106.      */
  107.     long now = System.currentTimeMillis();
  108.     if (Math.abs(now - lastExit) < HYSTERESIS) {
  109.         // if within the window pretend that it was played.
  110.         hasPlayed = true;
  111.             return;
  112.     }
  113.  
  114.     // Else play the sound.
  115.     if (! hasPlayed && (soundData != null)) {
  116.         hasPlayed = true;
  117.         soundData.play();
  118.     }
  119.     }
  120.  
  121.     /**
  122.      * The exit method is called when the mouse leaves the area.
  123.      */
  124.     public void exit() {
  125.     if (hasPlayed) {
  126.         hasPlayed = false;
  127.         lastExit = System.currentTimeMillis(); // note the time of exit
  128.     }
  129.     }
  130. }
  131.