import java.awt.*; import java.awt.event.*; import java.applet.*; public class Keyboard extends Panel implements KeyListener, FocusListener { private final Applet myApplet; private final int myNumNotes; private final float myFirstFrequency; private boolean iHaveFocus = false; public Keyboard( Applet applet, float firstNote, int numNotes ) throws Exception { myApplet = applet; myNumNotes = numNotes; myFirstFrequency = firstNote; int[] keys = new int[numNotes + 1]; for( int i = 0; i < numNotes + 1; i++ ) keys[ i ] = KeyEvent.VK_1 + i; initialize( keys ); } public Keyboard( Applet applet, float firstNote, int[] keys ) throws Exception { myApplet = applet; myNumNotes = keys.length - 1; myFirstFrequency = firstNote; initialize( keys ); } protected void initialize( int[] keys ) throws Exception { setVisible( false ); setBounds( myApplet.getBounds() ); setBackground( Color.lightGray ); setForeground( Color.lightGray ); setLayout( new GridLayout( 1, myNumNotes + 1, 1, 1 ) ); if( myNumNotes < 1 ) throw( new Exception( "Not enough notes!" ) ); myApplet.showStatus( " Getting sounds from docbase " + myApplet.getDocumentBase().toString() ); build( keys ); addKeyListener( this ); addFocusListener( this ); setVisible( true ); } protected final int numberOfNotes() { return myNumNotes; } protected final Applet theApplet() { return myApplet; } protected void build( int[] keys ) { try { float freq = myFirstFrequency; final float ratio = (float)Math.pow( 2.0, 1.0 / (float)numberOfNotes() ); for( int i = 0; i < numberOfNotes() + 1; i++ ) { myApplet.showStatus( "Getting sound file " + String.valueOf( Math.round( freq ) ) + ".au" ); add( new Key( Math.round( freq ), keys[ i ], myApplet ) ); freq *= ratio; } } catch( Exception e ) { System.err.println( "Keyboard caught exception: " + e.toString() ); } } public Insets getInsets() // Container method { return new Insets( 4, 20, 4, 20 ); } public void focusGained( FocusEvent e ) // FocusListener method { iHaveFocus = true; setBackground( Color.darkGray ); setForeground( Color.darkGray ); repaint(); } public void focusLost( FocusEvent e ) // FocusListener method { iHaveFocus = false; setBackground( Color.lightGray ); setForeground( Color.lightGray ); repaint(); } public void paint( Graphics g ) // Component method { super.paint( g ); } protected void processKeyEvent(KeyEvent e) { System.err.println( "processKeyEvent " + e.toString() ); super.processKeyEvent(e); } public void keyPressed( KeyEvent e ) // KeyListener method { System.err.println( "Key pressed " + e.toString() ); doKeyDown( e, true ); } public void keyReleased( KeyEvent e ) // KeyListener method { doKeyDown( e, false ); } private void doKeyDown( KeyEvent e, boolean down ) { final Component[] component = getComponents(); Key key; for( int i = 0; i < component.length; i++ ) if( component[i] instanceof Key ) { key = (Key)component[i]; if( key.keyCode() == e.getKeyCode() && ( down && !key.isPressed() || !down && key.isPressed() ) ) { key.play( down ); key.setKeyPressed( down ); break; } } } public void keyTyped( KeyEvent e ) // KeyListener method { System.err.println( "Key typed " + e.toString() ); } }