/Users/lyon/j4p/src/sound/soundDemo/OuterPiano.java
|
1 /**
2 * Created by IntelliJ IDEA.
3 * User: dlyon
4 * Date: Sep 29, 2003
5 * Time: 8:53:01 PM
6 * To change this template use Options | File Templates.
7 */
8 package sound.soundDemo;
9
10 import javax.swing.*;
11 import java.awt.event.MouseListener;
12 import java.awt.event.MouseMotionAdapter;
13 import java.awt.event.MouseEvent;
14 import java.awt.*;
15 import java.util.Vector;
16
17 /**
18 * Piano renders black & white keys and plays the notes for a MIDI
19 * channel.
20 */
21 class OuterPiano extends JPanel implements MouseListener {
22
23 Vector blackKeys = new Vector();
24 Key prevKey;
25 final int kw = 16, kh = 80;
26 private MidiSynth midiSynth;
27
28 public OuterPiano(final MidiSynth midiSynth) {
29 this.midiSynth = midiSynth;
30 midiSynth.setLayout(new BorderLayout());
31 midiSynth.setPreferredSize(new Dimension(42 * kw, kh + 1));
32 int transpose = 24;
33 int whiteIDs[] = {0, 2, 4, 5, 7, 9, 11};
34
35 for (int i = 0, x = 0; i < 6; i++) {
36 for (int j = 0; j < 7; j++, x += kw) {
37 int keyNum = i * 12 + whiteIDs[j] + transpose;
38 Key k = new Key(midiSynth, x, 0, kw, kh, keyNum);
39 midiSynth.whiteKeys.add(k);
40 }
41 }
42 for (int i = 0, x = 0; i < 6; i++, x += kw) {
43 int keyNum = i * 12 + transpose;
44 blackKeys.add(new Key(midiSynth, (x += kw) - 4, 0, kw / 2, kh / 2, keyNum + 1));
45 blackKeys.add(new Key(midiSynth, (x += kw) - 4, 0, kw / 2, kh / 2, keyNum + 3));
46 x += kw;
47 blackKeys.add(new Key(midiSynth, (x += kw) - 4, 0, kw / 2, kh / 2, keyNum + 6));
48 blackKeys.add(new Key(midiSynth, (x += kw) - 4, 0, kw / 2, kh / 2, keyNum + 8));
49 blackKeys.add(new Key(midiSynth, (x += kw) - 4, 0, kw / 2, kh / 2, keyNum + 10));
50 }
51 midiSynth.keys.addAll(blackKeys);
52 midiSynth.keys.addAll(midiSynth.whiteKeys);
53
54 midiSynth.addMouseMotionListener(new MouseMotionAdapter() {
55 public void mouseMoved(MouseEvent e) {
56 if (midiSynth.mouseOverCB.isSelected()) {
57 Key key = getKey(e.getPoint());
58 if (prevKey != null && prevKey != key) {
59 prevKey.off();
60 }
61 if (key != null && prevKey != key) {
62 key.on();
63 }
64 prevKey = key;
65 midiSynth.repaint();
66 }
67 }
68 });
69 midiSynth.addMouseListener(this);
70 }
71
72 public void mousePressed(MouseEvent e) {
73 prevKey = getKey(e.getPoint());
74 if (prevKey != null) {
75 prevKey.on();
76 midiSynth.repaint();
77 }
78 }
79
80 public void mouseReleased(MouseEvent e) {
81 if (prevKey != null) {
82 prevKey.off();
83 midiSynth.repaint();
84 }
85 }
86
87 public void mouseExited(MouseEvent e) {
88 if (prevKey != null) {
89 prevKey.off();
90 midiSynth.repaint();
91 prevKey = null;
92 }
93 }
94
95 public void mouseClicked(MouseEvent e) {
96 }
97
98 public void mouseEntered(MouseEvent e) {
99 }
100
101
102 public Key getKey(Point point) {
103 for (int i = 0; i < midiSynth.keys.size(); i++) {
104 if (((Key) midiSynth.keys.get(i)).contains(point)) {
105 return (Key) midiSynth.keys.get(i);
106 }
107 }
108 return null;
109 }
110
111 public void paint(Graphics g) {
112 Graphics2D g2 = (Graphics2D) g;
113 Dimension d = midiSynth.getSize();
114
115 g2.setBackground(midiSynth.getBackground());
116 g2.clearRect(0, 0, d.width, d.height);
117
118 g2.setColor(Color.white);
119 g2.fillRect(0, 0, 42 * kw, kh);
120
121 drawWhiteKeys(g2);
122 drawBlackKeys(g2);
123 }
124
125 private void drawWhiteKeys(Graphics2D g2) {
126 for (int i = 0; i < midiSynth.whiteKeys.size(); i++) {
127 Key key = (Key) midiSynth.whiteKeys.get(i);
128 if (key.isNoteOn())
129 drawWhiteKeyOn(g2, key);
130 drawWhiteKeyOff(g2, key);
131 }
132 }
133
134 private void drawWhiteKeyOff(Graphics2D g2, Key key) {
135 g2.setColor(Color.black);
136 g2.draw(key);
137 }
138
139 private void drawWhiteKeyOn(Graphics2D g2, Key key) {
140 Color c = midiSynth.record ? midiSynth.pink : midiSynth.jfcBlue;
141 g2.setColor(c);
142 g2.fill(key);
143 }
144
145 private void drawBlackKeys(Graphics2D g2) {
146 for (int i = 0; i < blackKeys.size(); i++) {
147 Key key = (Key) blackKeys.get(i);
148 if (key.isNoteOn())
149 drawKeyOn(g2, key);
150 else
151 drawKeyOff(g2, key);
152 }
153 }
154
155 private void drawKeyOff(Graphics2D g2, Key key) {
156 g2.setColor(Color.black);
157 g2.fill(key);
158 }
159
160 private void drawKeyOn(Graphics2D g2, Key key) {
161 g2.setColor(midiSynth.record ? midiSynth.pink : midiSynth.jfcBlue);
162 g2.fill(key);
163 g2.setColor(Color.black);
164 g2.draw(key);
165 }
166 } // End class Piano
167