-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPianoMouseListener.java
More file actions
69 lines (62 loc) · 1.78 KB
/
PianoMouseListener.java
File metadata and controls
69 lines (62 loc) · 1.78 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import javax.swing.*;
import java.awt.event.*;
import javax.sound.midi.*;
import java.util.*;
/**
* Handles mouse press, release, and drag events on the Piano.
*/
public class PianoMouseListener extends MouseAdapter {
private List<Key> _keys;
private List<Key> PressedOnKeys;
/**
* @param keys the list of keys in the piano.
*/
public PianoMouseListener (List<Key> keys) {
_keys = keys;
}
@Override
/**
* This method is called by Swing whenever the user drags the mouse.
* @param e the MouseEvent containing the (x,y) location, relative to the upper-left-hand corner
* of the entire piano, of where the mouse is currently located.
*/
public void mouseDragged (MouseEvent e) {
for (Key key : _keys) {
if (key.getPolygon().contains(e.getX(), e.getY())) {
if (!(key.on())) {
key.keyPressed();
key.play(true);
}
} else {
key.play(false);
key.keyReleased();
}
}
}
@Override
/**
* This method is called by Swing whenever the user presses the mouse.
* @param e the MouseEvent containing the (x,y) location, relative to the upper-left-hand corner
* of the entire piano, of where the mouse is currently located.
*/
public void mousePressed (MouseEvent e) {
for (Key key : _keys) {
if (key.getPolygon().contains(e.getX(), e.getY())) {
key.play(true);
key.keyPressed();
}
}
}
@Override
/**
* This method is called by Swing whenever the user releases the mouse.
* @param e the MouseEvent containing the (x,y) location, relative to the upper-left-hand corner
* of the entire piano, of where the mouse is currently located.
*/
public void mouseReleased (MouseEvent e) {
for (Key key : _keys) {
key.play(false);
key.keyReleased();
}
}
}