home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / plugin / applets / ImageMap / DelayedSoundArea.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  3.9 KB  |  121 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.  * @(#)DelayedSoundArea.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. import java.util.StringTokenizer;
  45.  
  46. /**
  47.  * This ImageArea Class will play a sound each time the user enters the 
  48.  * area. It is different from SoundArea in that it accepts a delay (in 
  49.  * tenths of a second) before it plays the sound. If the mouse leaves
  50.  * the area before the time delay, the sound is not played.
  51.  *
  52.  * This allows you to have one piece of audio when the button is "hit"
  53.  * via SoundArea and another if the user stays on the button.
  54.  *
  55.  * @author     Chuck McManis
  56.  * @version     1.12, 06/13/02
  57.  */
  58. class DelayedSoundArea extends ImageMapArea {
  59.     /** The URL of the sound to be played. */
  60.     URL     sound;
  61.     AudioClip    soundData;
  62.     boolean     hasPlayed; 
  63.     int            delay;
  64.     int        countDown;
  65.  
  66.     /**
  67.      * The argument is the URL of the sound to be played.
  68.      * This method also sets this type of area to be non-terminal.
  69.      */
  70.     public void handleArg(String arg) {
  71.     Thread soundLoader;
  72.     StringTokenizer st = new StringTokenizer(arg, ", ");
  73.     
  74.     delay = Integer.parseInt(st.nextToken());
  75.     try {
  76.         sound = new URL(parent.getDocumentBase(), st.nextToken());
  77.     } catch (MalformedURLException e) {
  78.         sound = null;
  79.     }
  80.     }
  81.  
  82.     public void getMedia() {
  83.     if (sound != null) {
  84.         soundData = parent.getAudioClip(sound);
  85.     }
  86.     if (soundData == null) {
  87.         System.out.println("DelayedSoundArea: Unable to load data "+sound);
  88.     }
  89.     }
  90.  
  91.     /**
  92.      * The highlight method plays the sound in addition to the usual
  93.      * graphical highlight feedback.
  94.      */
  95.     public void enter() {
  96.     hasPlayed = false;
  97.     countDown = delay;
  98.     parent.startAnimation();
  99.     }
  100.  
  101.     /**
  102.      * This method is called every animation cycle if there are any
  103.      * active animating areas.
  104.      * @return true if this area requires further animation notifications
  105.      */
  106.     public boolean animate() {
  107.     if (entered && ! hasPlayed) {
  108.         if (countDown > 0) {
  109.         countDown--;
  110.         return true;
  111.         }
  112.         hasPlayed = true;
  113.         if (soundData != null) {
  114.             soundData.play();
  115.         }
  116.     }
  117.     return false;
  118.     }
  119. }
  120.  
  121.