import java.awt.*; public class Guitar extends java.applet.Applet { Font f = new Font("TimesRoman", Font.BOLD, 36); Font fsmall = new Font("TimesRoman", Font.PLAIN, 14); FlowLayout layoutman = new FlowLayout(FlowLayout.RIGHT); List chords = new List(5,false); Label chordDescrip = new Label("Please select a chord"); Label chordDescrip2 = new Label("to view from the list box."); Button go = new Button("View Chord"); final int fretx = 25; final int frety = 65; int chordcode = 0; void DrawFretBoard(Graphics screen) { int i, j; screen.setColor(Color.black); screen.fillRect(20, 60, 100, 5); // outline of fret board 25, 65 --> 90 wide 200 tall // draw indiv frets for (j=0; j < 4; j++) for (i=0; i < 6; i++) screen.drawRect(fretx, frety+(j*50), (i*18), 50); } void DrawBoard(Graphics screen, int [][] board ) { Integer num; int i, j; for (i=0; i < 4; i++) for (j=0; j < 6; j++) if (board[i][j] != 0) { screen.setColor(Color.blue); screen.fillOval(fretx-10+(j*18),frety+15+(i*50), 20, 20); screen.setFont(fsmall); screen.setColor(Color.white); num = new Integer(board[i][j]); screen.drawString(num.toString(), fretx-2+(j*18),frety+30+(i*50) ); } } void DrawFingerings(Graphics screen) { // fingerings are given by a 4X6 matrix int [][] board = new int [4][6]; switch( chordcode ) { case 0: // A board[0] = new int [] { 0, 0, 0, 0, 0, 0 }; board[1] = new int [] { 0, 0, 2, 1, 3, 0 }; board[2] = new int [] { 0, 0, 0, 0, 0, 0 }; board[3] = new int [] { 0, 0, 0, 0, 0, 0 }; break; case 1: // Am board[0] = new int [] { 0, 0, 0, 0, 1, 0 }; board[1] = new int [] { 0, 0, 2, 3, 0, 0 }; board[2] = new int [] { 0, 0, 0, 0, 0, 0 }; board[3] = new int [] { 0, 0, 0, 0, 0, 0 }; break; case 2: // B board[0] = new int [] { 0, 0, 0, 0, 0, 0 }; board[1] = new int [] { 0, 0, 0, 0, 0, 1 }; board[2] = new int [] { 0, 0, 0, 0, 0, 0 }; board[3] = new int [] { 0, 0, 2, 3, 4, 0 }; break; case 3: // Bm board[0] = new int [] { 0, 0, 0, 0, 0, 0 }; board[1] = new int [] { 0, 0, 0, 0, 0, 1 }; board[2] = new int [] { 0, 0, 0, 0, 2, 0 }; board[3] = new int [] { 0, 0, 3, 4, 0, 0 }; break; case 4: // C board[0] = new int [] { 0, 0, 0, 0, 1, 0 }; board[1] = new int [] { 0, 0, 2, 0, 0, 0 }; board[2] = new int [] { 0, 3, 0, 0, 0, 0 }; board[3] = new int [] { 0, 0, 0, 0, 0, 0 }; break; case 5: // Cm board[0] = new int [] { 0, 0, 0, 0, 0, 0 }; board[1] = new int [] { 0, 0, 0, 0, 0, 0 }; board[2] = new int [] { 0, 0, 0, 0, 0, 1 }; board[3] = new int [] { 0, 0, 0, 0, 2, 0 }; break; case 6: // D board[0] = new int [] { 0, 0, 0, 0, 0, 0 }; board[1] = new int [] { 0, 0, 0, 1, 0, 2 }; board[2] = new int [] { 0, 0, 0, 0, 3, 0 }; board[3] = new int [] { 0, 0, 0, 0, 0, 0 }; break; case 7: // Dm board[0] = new int [] { 0, 0, 0, 0, 0, 1 }; board[1] = new int [] { 0, 0, 0, 2, 0, 0 }; board[2] = new int [] { 0, 0, 0, 0, 4, 0 }; board[3] = new int [] { 0, 0, 0, 0, 0, 0 }; break; case 8: // E board[0] = new int [] { 0, 0, 1, 0, 0, 0 }; board[1] = new int [] { 0, 2, 3, 0, 0, 0 }; board[2] = new int [] { 0, 0, 0, 0, 0, 0 }; board[3] = new int [] { 0, 0, 0, 0, 0, 0 }; break; case 9: // Em board[0] = new int [] { 0, 0, 0, 0, 0, 0 }; board[1] = new int [] { 0, 2, 3, 0, 0, 0 }; board[2] = new int [] { 0, 0, 0, 0, 0, 0 }; board[3] = new int [] { 0, 0, 0, 0, 0, 0 }; break; case 10: // F board[0] = new int [] { 0, 0, 0, 0, 1, 1 }; board[1] = new int [] { 0, 0, 0, 2, 0, 0 }; board[2] = new int [] { 0, 0, 3, 0, 0, 0 }; board[3] = new int [] { 0, 0, 0, 0, 0, 0 }; break; case 11: // Fm board[0] = new int [] { 0, 0, 0, 1, 1, 1 }; board[1] = new int [] { 0, 0, 0, 0, 0, 0 }; board[2] = new int [] { 0, 0, 3, 0, 0, 0 }; board[3] = new int [] { 0, 0, 0, 0, 0, 0 }; break; case 12: // G board[0] = new int [] { 0, 0, 0, 0, 0, 0 }; board[1] = new int [] { 0, 2, 0, 0, 0, 0 }; board[2] = new int [] { 3, 0, 0, 0, 0, 4 }; board[3] = new int [] { 0, 0, 0, 0, 0, 0 }; break; case 13: // Gm board[0] = new int [] { 0, 0, 0, 0, 0, 0 }; board[1] = new int [] { 0, 0, 0, 0, 0, 0 }; board[2] = new int [] { 0, 0, 0, 1, 1, 1 }; board[3] = new int [] { 0, 0, 0, 0, 0, 0 }; break; } DrawBoard(screen, board ); } // takes care of the drawing events in the applet // ie. the fretboard public void paint(Graphics screen) { int chordcode; screen.setFont(f); screen.setColor(Color.red); screen.drawString("Fretboard", 150, 240); DrawFretBoard(screen); DrawFingerings(screen); } // event handler for my applet -- only 1 event is caught public boolean action(Event evt, Object arg) { if (evt.target instanceof Button) { // if button is pressed then display new fingering chordcode = chords.getSelectedIndex(); repaint(); return true; } else if (evt.target instanceof List) { // a chord is selected from list return true; } else return false; } // initializer for my applet, // it sets up the layout and adds components public void init() { setLayout(layoutman); add(chordDescrip); add(chordDescrip2); add(go); chords.addItem("A"); chords.addItem("Am"); chords.addItem("B"); chords.addItem("Bm"); chords.addItem("C"); chords.addItem("Cm"); chords.addItem("D"); chords.addItem("Dm"); chords.addItem("E"); chords.addItem("Em"); chords.addItem("F"); chords.addItem("Fm"); chords.addItem("G"); chords.addItem("Gm"); add(chords); //chords.select(0); } }