KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DelayedSoundArea


1 /*
2  * @(#)DelayedSoundArea.java 1.17 06/02/22
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)DelayedSoundArea.java 1.17 06/02/22
39  */

40
41 import java.awt.Graphics JavaDoc;
42 import java.applet.AudioClip JavaDoc;
43 import java.net.URL JavaDoc;
44 import java.net.MalformedURLException JavaDoc;
45 import java.util.StringTokenizer JavaDoc;
46
47 /**
48  * This ImageArea Class will play a sound each time the user enters the
49  * area. It is different from SoundArea in that it accepts a delay (in
50  * tenths of a second) before it plays the sound. If the mouse leaves
51  * the area before the time delay, the sound is not played.
52  *
53  * This allows you to have one piece of audio when the button is "hit"
54  * via SoundArea and another if the user stays on the button.
55  *
56  * @author Chuck McManis
57  * @version 1.17, 02/22/06
58  */

59 class DelayedSoundArea extends ImageMapArea {
60     /** The URL of the sound to be played. */
61     URL JavaDoc sound;
62     AudioClip JavaDoc soundData;
63     boolean hasPlayed;
64     int delay;
65     int countDown;
66
67     /**
68      * The argument is the URL of the sound to be played.
69      * This method also sets this type of area to be non-terminal.
70      */

71     public void handleArg(String JavaDoc arg) {
72     Thread JavaDoc soundLoader;
73     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(arg, ", ");
74     
75     delay = Integer.parseInt(st.nextToken());
76     try {
77         sound = new URL JavaDoc(parent.getDocumentBase(), st.nextToken());
78     } catch (MalformedURLException JavaDoc e) {
79         sound = null;
80     }
81     }
82
83     public void getMedia() {
84     if (sound != null) {
85         soundData = parent.getAudioClip(sound);
86     }
87     if (soundData == null) {
88         System.out.println("DelayedSoundArea: Unable to load data "+sound);
89     }
90     }
91
92     /**
93      * The highlight method plays the sound in addition to the usual
94      * graphical highlight feedback.
95      */

96     public void enter() {
97     hasPlayed = false;
98     countDown = delay;
99     parent.startAnimation();
100     }
101
102     /**
103      * This method is called every animation cycle if there are any
104      * active animating areas.
105      * @return true if this area requires further animation notifications
106      */

107     public boolean animate() {
108     if (entered && ! hasPlayed) {
109         if (countDown > 0) {
110         countDown--;
111         return true;
112         }
113         hasPlayed = true;
114         if (soundData != null) {
115             soundData.play();
116         }
117     }
118     return false;
119     }
120 }
121
122
Popular Tags