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