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