1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
public class MyApplet extends Applet implements MouseListener {
/** Map that indexes clips by shapes.
*/
private Map<Rectangle, AudioClip> rectSoundMap = new HashMap<Rectangle, AudioClip>();
/** Creates a new instance.
*/
public MyApplet() {
super();
addMouseListener(this);
}
// Creates a rectangle.
Rectangle rect = new Rectangle(0, 0, 100, 50);
// Gets a clip. See Applet documentation for parameter info.
AudioClip clip = getAudioClip("a.wav");
// Associate the rectangle to the map.
rectSoundMap.put(rect, sound);
/** @inheritDoc
*/
public void mouseClicked(MouseEvent event) {
// Iterate through all available rectangles.
for (Rectangle rect : rectSoundMap.keys()) {
// Verify we're inside.
if (rect.contains(event.getX(), event.getY())) {
// Get clip associated to this rectagle.
AudioClip clip = rectSoundMap.get(rect);
// And play it.
clip.play();
// Exit loop.
break;
}
}
}
} |
Partager