1 package chromesthesia2; 2 3 import java.util.Scanner; 4 import javax.swing.JOptionPane; 5 import javax.swing.SwingUtilities; 6 import painter.SPainter; 7 //This program is the third and last of the Chromesthesia Programs. 8 //This program has a different color set up. 9 //Letters A B C D E F G are understood with number time indications like 1 2 3 1/2 1/3 and 2/3 10 //This program has an Again function. 11 //Location package chromesthesia2 12 public class Chromesthesia { //1 13 14 public static void main(String[] args) { //2 15 16 SwingUtilities.invokeLater(new chromesthesia2.Chromesthesia.ThreadForGUI());//3 17 } 18 private static class ThreadForGUI implements Runnable { //4 19 @Override //5 20 public void run() { //6 21 new chromesthesia2.Chromesthesia(); //7 22 } //8 23 } 24 public Chromesthesia() { //checked 25 interpreter(); //checked 26 } 27 //Featured Variables... 28 private static SPainter leo; //Painter 29 private static chromesthesia2.Pitch[] pitches; //Array 30 31 //Interpreter 32 private static void interpreter() { //found a mistake... fixed 33 initialization(); 34 String memory = "will change"; 35 while (true) { //8 36 37 String input = getInput(); //9 38 if ( input.equalsIgnoreCase("Exit")) { //this works fine 39 break;//break10 40 } else if (input.equalsIgnoreCase("Again")) { 41 try { 42 playMelody(memory,pitches); 43 } catch(Exception ex) { //14 44 showErrorMessage(ex.toString()); //Shows error Message 45 } 46 } else { //11 47 try { //12 48 memory = input; 49 playMelody(input,pitches); //13 50 } catch(Exception ex) { //14 51 showErrorMessage(ex.toString()); //Shows error Message 52 } //uno 53 } //dos 54 } //tres 55 cleanup(); //o^o 56 } //13 57 58 59 //Pitches OH god.-. 60 private static chromesthesia2.Pitch[] establishPitches(SPainter painter) { //Correct 61 chromesthesia2.Pitch[] pitches = new chromesthesia2.Pitch[21]; //Correct 62 chromesthesia2.Pitch pitchMiddleC = new chromesthesia2.Pitch("C",painter); //Correct 63 pitches[0] = pitchMiddleC; //Correct 64 65 chromesthesia2.Pitch pitchLowC = new chromesthesia2.Pitch("C,",painter); //Correct 66 pitches[1] = pitchLowC; //Correct 67 68 chromesthesia2.Pitch pitchHighC = new chromesthesia2.Pitch("c",painter); //Correct 69 pitches[2] = pitchHighC; //Correct 70 71 chromesthesia2.Pitch pitchMiddleD = new chromesthesia2.Pitch("D",painter); //Correct 72 pitches[3] = pitchMiddleD; //Correct 73 chromesthesia2.Pitch pitchLowD = new chromesthesia2.Pitch("D,",painter); //Found an error 0-0... fixed 74 pitches[4] = pitchLowD; //Correct 75 chromesthesia2.Pitch pitchHighD = new chromesthesia2.Pitch("d",painter); //Correct 76 pitches[5] = pitchHighD; //Correct 77 78 chromesthesia2.Pitch pitchMiddleE = new chromesthesia2.Pitch("E",painter); //Correct 79 pitches[6] = pitchMiddleE; //Correct 80 chromesthesia2.Pitch pitchLowE = new chromesthesia2.Pitch("E,",painter); //Another error ,-, 81 pitches[7] = pitchLowE; //Correct 82 chromesthesia2.Pitch pitchHighE = new chromesthesia2.Pitch("e",painter); //Correct 83 pitches[8] = pitchHighE; //Correct 84 85 chromesthesia2.Pitch pitchMiddleF = new chromesthesia2.Pitch("F",painter); //added 86 pitches[9] = pitchMiddleF; //added 87 chromesthesia2.Pitch pitchLowF = new chromesthesia2.Pitch("F,",painter); //added 88 pitches[10] = pitchLowF; //added 89 chromesthesia2.Pitch pitchHighF = new chromesthesia2.Pitch("f",painter); //added 90 pitches[11] = pitchHighF; //added 91 92 chromesthesia2.Pitch pitchMiddleG = new chromesthesia2.Pitch("G",painter); //added 93 pitches[12] = pitchMiddleG; //added 94 chromesthesia2.Pitch pitchLowG = new chromesthesia2.Pitch("G,",painter); //added 95 pitches[13] = pitchLowG; //added 96 chromesthesia2.Pitch pitchHighG = new chromesthesia2.Pitch("g",painter); //added 97 pitches[14] = pitchHighG; //added 98 99 chromesthesia2.Pitch pitchMiddleA = new chromesthesia2.Pitch("A",painter); //added 100 pitches[15] = pitchMiddleA; //added 101 chromesthesia2.Pitch pitchLowA = new chromesthesia2.Pitch("A,",painter); //added 102 pitches[16] = pitchLowA; //added 103 chromesthesia2.Pitch pitchHighA = new chromesthesia2.Pitch("a",painter); //added 104 pitches[17] = pitchHighA; //added 105 106 chromesthesia2.Pitch pitchMiddleB = new chromesthesia2.Pitch("B",painter); //added 107 pitches[18] = pitchMiddleB; //added 108 chromesthesia2.Pitch pitchLowB = new chromesthesia2.Pitch("B,",painter); //added 109 pitches[19] = pitchLowB; //added 110 chromesthesia2.Pitch pitchHighB = new chromesthesia2.Pitch("b",painter); //added 111 pitches[20] = pitchHighB; //added 112 113 return pitches; //Correct 114 } //hmm kinda working now abc notation is a weird name for spacing!!! 115 116 private static chromesthesia2.Pitch find(String token, chromesthesia2.Pitch[] pitches) throws Exception { 117 for (int i = 0; i < pitches.length; i = i + 1) { 118 chromesthesia2.Pitch pitch = pitches[i]; 119 if (pitch.abcName().equals(token) ) { 120 return pitch; 121 } 122 } 123 throw new Exception("### PITCH" + token + " NOT FOUND"); 124 } 125 private static void display(chromesthesia2.Pitch[] pitches) { 126 for (int i = 0; i < pitches.length; i = i + 1) { 127 System.out.println(pitches[i].toString()); 128 } 129 } 130 private static void playMelody(String input, Pitch[] pitches) throws Exception { 131 Scanner scanner = new Scanner(input); 132 while ( scanner.hasNext() ) { 133 String token = scanner.next(); 134 String pitchName; 135 String duration = ""; 136 if ( token.indexOf(",") < 0 ) { 137 pitchName = token.substring(0,1); 138 duration = token.substring(1); 139 } else { 140 pitchName = token.substring(0,2); 141 duration = token.substring(2); 142 } 143 if ( duration.length() == 0 ) { duration = "1"; } 144 Pitch pitch = find(pitchName,pitches); 145 pitch.play(duration); 146 } 147 } 148 static private void showErrorMessage(String message) { 149 leo.setVisible(false); 150 JOptionPane.showMessageDialog(null,message); 151 } 152 private static void initialization() { 153 leo = new SPainter("Chromesthesia",500,500); 154 leo.setVisible(false); 155 leo.setBrushWidth(7); 156 pitches = establishPitches(leo); 157 display(pitches); 158 } 159 private static String getInput() { 160 leo.setVisible(false); 161 String label = "Please enter a melody in abc notation, Again, or EXIT..."; 162 String input = JOptionPane.showInputDialog(null,label); 163 leo.setVisible(true); 164 if (input == null) { 165 input = ""; 166 } 167 return input; 168 } 169 170 static private void cleanup() { 171 System.exit(0); 172 } 173 } 174