/* * This program will have specific color to the lab manual and have * different notes like 1/2 or 2 or 2/3 and 3 */ package chromesthesia2; import java.util.Scanner; import javax.swing.JOptionPane; import javax.swing.SwingUtilities; import painter.SPainter; public class Chromesthesia { public static void main(String[] args) { SwingUtilities.invokeLater(new ThreadForGUI()); } private static class ThreadForGUI implements Runnable { @Override public void run() { new Chromesthesia(); } } public Chromesthesia() { interpreter(); } private static SPainter miro; private static Pitch[] pitches; public static void interpreter() { initialization(); // miro and pitches while ( true ) { String input = getInput(); if ( input.equalsIgnoreCase("EXIT") ) { break; } else { try { playMelody(input,pitches); } catch (Exception ex) { showErrorMessage(ex.toString()); } } } cleanup(); // miro has to go } private static Pitch[] establishPitches(SPainter painter) { Pitch[] pitches = new Pitch[21]; Pitch pitchMiddleA = new Pitch("A",painter); pitches[0] = pitchMiddleA; Pitch pitchLowA = new Pitch("A,",painter); pitches[1] = pitchLowA; Pitch pitchHigha = new Pitch("a",painter); pitches[2] = pitchHigha; Pitch pitchMiddleB = new Pitch("B",painter); pitches[3] = pitchMiddleB; Pitch pitchLowB = new Pitch("B,",painter); pitches[4] = pitchLowB; Pitch pitchHighb = new Pitch("b",painter); pitches[5] = pitchHighb; Pitch pitchMiddleC = new Pitch("C",painter); pitches[6] = pitchMiddleC; Pitch pitchLowC = new Pitch("C,",painter); pitches[7] = pitchLowC; Pitch pitchHighC = new Pitch("c",painter); pitches[8] = pitchHighC; Pitch pitchMiddleD = new Pitch("D",painter); pitches[9] = pitchMiddleD; Pitch pitchLowD = new Pitch("D,",painter); pitches[10] = pitchLowD; Pitch pitchHighD = new Pitch("d",painter); pitches[11] = pitchHighD; Pitch pitchMiddleE = new Pitch("E",painter); pitches[12] = pitchMiddleE; Pitch pitchLowE = new Pitch("E,",painter); pitches[13] = pitchLowE; Pitch pitchHighE = new Pitch("e",painter); pitches[14] = pitchHighE; Pitch pitchMiddleF = new Pitch("F",painter); pitches[15] = pitchMiddleF; Pitch pitchLowF = new Pitch("F,",painter); pitches[16] = pitchLowF; Pitch pitchHighf = new Pitch("f",painter); pitches[17] = pitchHighf; Pitch pitchMiddleG = new Pitch("G",painter); pitches[18] = pitchMiddleG; Pitch pitchLowG = new Pitch("G,",painter); pitches[19] = pitchLowG; Pitch pitchHighg = new Pitch("g",painter); pitches[20] = pitchHighg; return pitches; } private static Pitch find(String token, Pitch[] pitches) throws Exception { for ( int i = 0; i < pitches.length; i = i + 1 ) { Pitch pitch = pitches[i]; if ( pitch.abcName().equals(token) ) { return pitch; } } throw new Exception("### PITCH " + token + " NOT FOUND"); } private static void display(Pitch[] pitches) { for ( int i = 0; i < pitches.length; i = i + 1 ) { System.out.println(pitches[i].toString()); } } private static void playMelody(String input, Pitch[] pitches) throws Exception { Scanner scanner = new Scanner(input); while ( scanner.hasNext() ) { String token = scanner.next(); String pitchName; String duration = ""; if ( token.indexOf(",") < 0 ) { pitchName = token.substring(0,1); duration = token.substring(1); } else { pitchName = token.substring(0,2); duration = token.substring(2); } if ( duration.length() == 0 ) { duration = "1"; } Pitch pitch = find(pitchName,pitches); pitch.play(duration); } } static private void showErrorMessage(String message) { miro.setVisible(false); JOptionPane.showMessageDialog(null, message); } private static void initialization() { miro = new SPainter("Chromesthesia",500,500); miro.setVisible(false); miro.setBrushWidth(7); pitches = establishPitches(miro); display(pitches); } private static String getInput() { miro.setVisible(false); String label = "Please enter a melody in ABC notation, or EXIT ... "; String input = JOptionPane.showInputDialog(null,label); miro.setVisible(true); if ( input == null ) { input = ""; } return input; } static private void cleanup() { System.exit(0); } }