import java.applet.Applet; import java.awt.event.*; import java.awt.*; import javax.sound.midi.*; public class Klaver extends Applet implements KeyListener, MouseListener{ MidiChannel kanal; public Klaver(){ addKeyListener(this); addMouseListener(this); try{ Synthesizer synthesizer=MidiSystem.getSynthesizer(); synthesizer.open(); kanal=synthesizer.getChannels()[0];} catch (Exception viga){} } public void mangi(int noot){ try{ kanal.noteOn(noot, 127); Thread.sleep(100); kanal.noteOff(noot); }catch(Exception w){}; } public void mousePressed(MouseEvent e){ int x=((int)(e.getX()/25)); int y=((int)(e.getY()/25)); int noot=0; if (y>3 & y<8 & x>3 & x<16){ switch(x){ case 4: noot=55; break; case 5: noot=56; break; case 6: noot=57; break; case 7: noot=58; break; case 8: noot=59; break; case 9: noot=60; break; case 10: noot=61; break; case 11: noot=62; break; case 12: noot=63; break; case 13: noot=64; break; case 14: noot=65; break; case 15: noot=66; break; } mangi(noot);} System.out.println(x+" "+y); } public void mouseReleased(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void paint (Graphics g){ for (int f=0;f<12;f++){ g.drawRect(100+f*25,100,25,100); switch(f){ case 1: g.setColor(Color.black);g.fillRect(100+f*25,100,25,100); break; case 3: g.setColor(Color.black);g.fillRect(100+f*25,100,25,100); break; case 6: g.setColor(Color.black);g.fillRect(100+f*25,100,25,100); break; case 8: g.setColor(Color.black);g.fillRect(100+f*25,100,25,100); break; case 10: g.setColor(Color.black);g.fillRect(100+f*25,100,25,100); break; } } } public void keyPressed(KeyEvent e){ int kood=e.getKeyCode(); try{ kanal.noteOn(kood, 127); Thread.sleep(100); kanal.noteOff(kood);} catch(Exception w){} System.out.println(kood+""); } public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} public static void main(String argumendid[]) throws Exception{ Frame f=new Frame("Klaver"); f.setSize(700, 600); f.add(new Klaver()); f.setVisible(true); f.addWindowListener(new Kinni()); } } class Kinni extends WindowAdapter{ public void windowClosing(WindowEvent e){ System.exit(0); } }