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 VARIABLES 28 29 private static SPainter miro; 30 private static Pitch[] pitches; 31 32 // THE INTERPRETER 33 34 public static void interpreter() { 35 36 initializaiton(); // 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 55 // METHODS PERTAINING TO THE CHROMESTHETIC PITCHES 56 57 private static Pitch[] establishPitches(SPainter painter) { 58 Pitch[] pitches = new Pitch[21]; 59 Pitch pitchMiddleC = new Pitch("C", painter); 60 pitches[0] = pitchMiddleC; 61 Pitch pitchLowC = new Pitch("C,", painter); 62 pitches[1] = pitchLowC; 63 Pitch pitchHighC = new Pitch("c", painter); 64 pitches[2] = pitchHighC; 65 Pitch pitchMiddleD = new Pitch("D", painter); 66 pitches[3] = pitchMiddleD; 67 Pitch pitchLowD = new Pitch("D,", painter); 68 pitches[4] = pitchLowD; 69 Pitch pitchHighD = new Pitch("d", painter); 70 pitches[5] = pitchHighD; 71 Pitch pitchMiddleE = new Pitch("E", painter); 72 pitches[6] = pitchMiddleE; 73 Pitch pitchLowE = new Pitch("E,", painter); 74 pitches[7] = pitchLowE; 75 Pitch pitchHighE = new Pitch("e", painter); 76 pitches[8] = pitchHighE; 77 Pitch pitchMiddleF = new Pitch("F", painter); 78 pitches[9] = pitchMiddleF; 79 Pitch pitchLowF = new Pitch("F,", painter); 80 pitches[10] = pitchLowF; 81 Pitch pitchHighF = new Pitch("f", painter); 82 pitches[11] = pitchHighF; 83 Pitch pitchMiddleG = new Pitch("G", painter); 84 pitches[12] = pitchMiddleG; 85 Pitch pitchLowG = new Pitch("G,", painter); 86 pitches[13] = pitchLowG; 87 Pitch pitchHighG = new Pitch("g", painter); 88 pitches[14] = pitchHighG; 89 Pitch pitchMiddleA = new Pitch("A", painter); 90 pitches[15] = pitchMiddleA; 91 Pitch pitchLowA = new Pitch("A,", painter); 92 pitches[16] = pitchLowA; 93 Pitch pitchHighA = new Pitch("a", painter); 94 pitches[17] = pitchHighA; 95 Pitch pitchMiddleB = new Pitch("B", painter); 96 pitches[18] = pitchMiddleB; 97 Pitch pitchLowB = new Pitch("B,", painter); 98 pitches[19] = pitchLowB; 99 Pitch pitchHighB = new Pitch("b", painter); 100 pitches[20] = pitchHighB; 101 return pitches; 102 } 103 104 private static Pitch find(String token, Pitch[] pitches) throws Exception { 105 for ( int i = 0; i < pitches.length; i = i + 1) { 106 Pitch pitch = pitches[i]; 107 if ( pitch.abcName().equals(token)){ 108 return pitch; 109 } 110 } 111 throw new Exception("### PITCH" + token + "NOT FOUND"); 112 } 113 114 private static void display(Pitch[] pitches) { 115 for ( int i = 0; i < pitches.length; i = i + 1 ) { 116 System.out.println(pitches[i].toString()); 117 } 118 } 119 120 private static void playMelody(String input, Pitch[] pitches) throws Exception { 121 Scanner scanner = new Scanner(input); 122 while ( scanner.hasNext()) { 123 String token = scanner.next(); 124 Pitch pitch = find(token, pitches); 125 pitch.play("1"); 126 } 127 } 128 129 // INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING 130 131 static private void showErrorMessage (String message) { 132 miro.setVisible(false); 133 JOptionPane.showMessageDialog(null, message); 134 } 135 136 private static void initializaiton() { 137 // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH 138 miro = new SPainter("Chromesthesia", 500,500); 139 miro.setVisible(false); 140 miro.setBrushWidth(7); 141 // ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS 142 pitches = establishPitches(miro); 143 display(pitches); 144 } 145 146 private static String getInput() { 147 miro.setVisible(false); 148 String label = "Please enter a melody in ABC notation, or EXIT... "; 149 String input = JOptionPane.showInputDialog(null, label); 150 miro.setVisible(true); 151 if ( input == null ) { input = "";} 152 return input; 153 } 154 155 static private void cleanup() { 156 System.exit(0); 157 } 158 } 159