1 2 package chromesthesia0; 3 4 import java.util.Scanner; 5 import javax.swing.JOptionPane; 6 import javax.swing.SwingUtilities; 7 import painter.SPainter; 8 9 public class Chromesthesia { 10 public static void main(String[] args) { 11 SwingUtilities.invokeLater( new ThreadForGUI() ); 12 } 13 14 private static class ThreadForGUI implements Runnable { 15 16 public void run() { 17 new Chromesthesia(); 18 } 19 } 20 21 public Chromesthesia() { 22 interpreter(); 23 } 24 25 // FEATURED VARIABLES 26 private static SPainter miro; 27 private static Pitch[] pitches; 28 29 // THE INTERPRETER 30 public static void interpreter() { 31 initialization(); // miro and pitches 32 while (true) { 33 String input = getInput(); 34 if (input.equalsIgnoreCase( "EXIT" )) { 35 break; 36 } else { 37 try { 38 playMelody( input, pitches ); 39 } catch (Exception ex) { 40 showErrorMessage( ex.toString() ); 41 } 42 } 43 } 44 cleanup(); // miro has to go 45 } 46 47 // METHODS PERTAINING TO THE CHROMESTHETIC PITCHES 48 private static Pitch[] establishPitches(SPainter painter) { 49 Pitch[] pitches = new Pitch[9]; 50 Pitch pitchMiddleC = new Pitch( "C", painter ); 51 pitches[0] = pitchMiddleC; 52 Pitch pitchLowC = new Pitch( "C,", painter ); 53 pitches[1] = pitchLowC; 54 Pitch pitchHighC = new Pitch( "c", painter ); 55 pitches[2] = pitchHighC; 56 Pitch pitchMiddleD = new Pitch( "D", painter ); 57 pitches[3] = pitchMiddleD; 58 Pitch pitchLowD = new Pitch( "D,", painter ); 59 pitches[4] = pitchLowD; 60 Pitch pitchHighD = new Pitch( "d", painter ); 61 pitches[5] = pitchHighD; 62 Pitch pitchMiddleE = new Pitch( "E", painter ); 63 pitches[6] = pitchMiddleE; 64 Pitch pitchLowE = new Pitch( "E,", painter ); 65 pitches[7] = pitchLowE; 66 Pitch pitchHighE = new Pitch( "e", painter ); 67 pitches[8] = pitchHighE; 68 return pitches; 69 } 70 71 private static Pitch find(String token, Pitch[] pitches) throws Exception { 72 for (int i = 0; i < pitches.length; i = i + 1) { 73 Pitch pitch = pitches[i]; 74 if (pitch.abcName().equals( token )) { 75 return pitch; 76 } 77 } 78 throw new Exception( "### PITCH " + token + " NOT FOUND" ); 79 } 80 81 private static void display(Pitch[] pitches) { 82 for (int i = 0; i < pitches.length; i = i + 1) { 83 System.out.println( pitches[i].toString() ); 84 } 85 } 86 87 private static void playMelody(String input, Pitch[] pitches) throws Exception { 88 Scanner scanner = new Scanner( input ); 89 while (scanner.hasNext()) { 90 String token = scanner.next(); 91 Pitch pitch = find( token, pitches ); 92 pitch.play( "1" ); 93 } 94 } 95 96 // INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING 97 static private void showErrorMessage(String message) { 98 miro.setVisible( false ); 99 JOptionPane.showMessageDialog( null, message ); 100 } 101 102 private static void initialization() { 103 104 miro = new SPainter( "Chromesthesia", 500, 500 ); 105 miro.setVisible( false ); 106 miro.setBrushWidth( 7 ); 107 108 pitches = establishPitches( miro ); 109 display( pitches ); 110 } 111 112 private static String getInput() { 113 miro.setVisible( false ); 114 String label = "Please enter a melody in ABC notation, or EXIT ... "; 115 String input = JOptionPane.showInputDialog( null, label ); 116 miro.setVisible( true ); 117 if (input == null) { 118 input = ""; 119 } 120 return input; 121 } 122 123 static private void cleanup() { 124 System.exit( 0 ); 125 } 126 127 } 128 129