-
Notifications
You must be signed in to change notification settings - Fork 0
/
Guiapplet.java
292 lines (260 loc) · 5.81 KB
/
Guiapplet.java
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import javax.sound.midi.*;
import javax.swing.*;
import java.applet.Applet;
import java.awt.event.*;
import java.awt.*;
import java.util.*;
public class Guiapplet extends JApplet
{
long time1 = System.currentTimeMillis();
static Gui gui;
private int width = Toolkit.getDefaultToolkit().getScreenSize().width;
public void start()
{
gui= new Gui();
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
gui.setOpaque(true);
setSize(width, 300);
setContentPane(gui);
}
});
}
catch (Exception e)
{
System.err.println("createGUI didn't complete successfully");
}
}
// MouseAdapter my= new MouseAdapter();
/* Playback logic after recording.
for(i=0; i<5; i++)
{
gui.play(60+i);
}
*/
//play(60, 4, 0);
public class Gui extends JPanel
{
int count=0;
JLabel oval;
JButton pl;
JToggleButton rec;
JButton[] array;
JButton[] black;
JButton demo;
Sequencer player;
Sequence seq;
Track track;
int flag=-1; // means not recording.
private JLayeredPane pane;
Queue<Integer> qe;
private int num;
Gui()
{
super();
this.pane= new JLayeredPane();
pane.setPreferredSize(new Dimension(width, 300));
pane.setBackground(Color.BLACK);
pane.setOpaque(true);
this.num = width/32;
array=new JButton[num];
black=new JButton[num];
int i;
for(i=0; i<num; i++)
{
array[i] = new JButton();
array[i].setOpaque(true);
array[i].setBorderPainted(false);
array[i].setBackground(Color.WHITE);
array[i].setBounds( 32*i, 30, 30, 300 );
pane.add(array[i], (Integer)15);
array[i].addMouseListener(new white());
}
for(i=0; i<num; i++)
{
int octave = i/5;
int note=i%5;
if(note>1)
note++;
black[i] = new JButton();
black[i].setOpaque(true);
black[i].setBorderPainted(false);
black[i].setBackground(Color.BLACK);
black[i].setBounds( 7*octave*32 + 19+32*note, 30, 15, 175 );
pane.add(black[i], (Integer)20);
black[i].addMouseListener(new black());
}
demo= new JButton("Demo");
demo.setBounds(100,0, 250, 30 );
pane.add(demo, (Integer) 0);
demo.addActionListener(new demo());
rec= new JToggleButton("Record");
rec.setBounds(450,0, 250, 30 );
pane.add(rec, (Integer) 0);
rec.addActionListener(new record());
pl= new JButton("Play");
pl.setBounds(850,0, 250, 30 );
pane.add(pl, (Integer) 0);
pl.addActionListener(new play());
this.add(pane);
try
{
player=MidiSystem.getSequencer();
player.open();
seq=new Sequence(Sequence.PPQ, 4);
track=seq.createTrack();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
class record implements ActionListener
{
public void actionPerformed(ActionEvent a)
{
if(flag==-1)
{
qe= new LinkedList<Integer>();
}
flag=flag*-1;
}
}
class play implements ActionListener
{
public void actionPerformed(ActionEvent a)
{
Iterator it=qe.iterator();
while(it.hasNext())
{
int iteratorValue=(Integer) it.next();
gui.play(iteratorValue);
}
}
}
class white implements MouseListener
{
public void mousePressed(MouseEvent event)
{
int i;
//System.out.println("presed"+ event.getSource());
for(i=0; i<num; i++)
{
if(event.getSource()==array[i])
{
array[i].setBackground(Color.RED);
int octave=i/7;
int note= i%7;
int offset=2*note;
if(note>2)
offset--;
gui.play(36+12*octave+offset);
if(flag==1)
{
qe.add((Integer)36+12*octave+offset);
}
// System.out.println(System.currentTimeMillis()/1000);
}
}
}
public void mouseClicked(MouseEvent arg0)
{
}
public void mouseEntered(MouseEvent arg0)
{
}
public void mouseExited(MouseEvent arg0)
{
}
public void mouseReleased(MouseEvent event)
{
int i;
for(i=0; i<num; i++)
{
if(event.getSource()==array[i])
array[i].setBackground(Color.WHITE);
}
}
}
class demo implements ActionListener
{
public void actionPerformed(ActionEvent a)
{
gui.play(60);gui.play(60);gui.play(62);gui.play(60);gui.play(65);gui.play(64);
count++;
gui.play(60);gui.play(60);gui.play(62);gui.play(60);gui.play(67);gui.play(65);
count++;
gui.play(60);gui.play(60);gui.play(69);gui.play(67);gui.play(65);gui.play(64);gui.play(62);
count++;
gui.play(69);gui.play(69);gui.play(67);gui.play(64);gui.play(67);gui.play(65);
}
}
class black implements MouseListener
{
public void mousePressed(MouseEvent event)
{
int i;
for(i=0; i<num; i++)
{
if(event.getSource()==black[i])
{
black[i].setBackground(Color.RED);
int octave = i/5;
int note = i%5;
int offset= 2*note;
if(note >1)
offset++;
gui.play(37+12*octave+offset);
if(flag==1)
{
qe.add((Integer)37+12*octave+offset);
}
}
}
}
public void mouseClicked(MouseEvent arg0)
{
}
public void mouseEntered(MouseEvent arg0)
{
}
public void mouseExited(MouseEvent arg0)
{
}
public void mouseReleased(MouseEvent event) {
int i;
for(i=0; i<num; i++)
{
if(event.getSource()==black[i])
black[i].setBackground(Color.BLACK);
}
}
}
public void play(int note)
{
try
{
ShortMessage a=new ShortMessage();
a.setMessage(144, 1, note, 127);
MidiEvent noteOn = new MidiEvent(a, 0+count*4);
track.add(noteOn);
ShortMessage b =new ShortMessage();
b.setMessage(128, 1, note, 127);
MidiEvent noteOff = new MidiEvent(b, 4+count*4);
track.add(noteOff);
count++;
player.setSequence(seq);
player.start();
// System.out.println(note);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
}