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