Chromesthesia.java
1    package chromesthesia0;
2    
3    import java.util.Scanner;
4    import javax.swing.JOptionPane;
5    import javax.swing.SwingUtilities;
6    import painter.SPainter;
7    //This is the most basic version of this program
8    //This program understands inputs of C D and E type notes.
9    //Each note type has an assigned Color.
10   //Have Fun...
11   public class Chromesthesia { //1
12   
13       public static void main(String[] args) { //2
14   
15           SwingUtilities.invokeLater(new ThreadForGUI());//3
16       }
17       private static class ThreadForGUI implements Runnable { //4
18           @Override //5
19           public void run() { //6
20               new 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 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 Pitch[] establishPitches(SPainter painter) { //Correct
52           Pitch[] pitches = new Pitch[9]; //Correct
53           Pitch pitchMiddleC = new Pitch ("C",painter); //Correct
54           pitches[0] = pitchMiddleC; //Correct
55   
56           Pitch pitchLowC = new Pitch("C,",painter); //Correct
57           pitches[1] = pitchLowC; //Correct
58   
59           Pitch pitchHighC = new Pitch("c",painter); //Correct
60           pitches[2] = pitchHighC; //Correct
61   
62           Pitch pitchMiddleD = new Pitch ("D",painter); //Correct
63           pitches[3] = pitchMiddleD; //Correct
64           Pitch pitchLowD = new Pitch("D,",painter); //Found an error 0-0... fixed
65           pitches[4] = pitchLowD; //Correct
66           Pitch pitchHighD = new Pitch("d",painter); //Correct
67           pitches[5] = pitchHighD; //Correct
68   
69           Pitch pitchMiddleE = new Pitch ("E",painter); //Correct
70           pitches[6] = pitchMiddleE; //Correct
71           Pitch pitchLowE = new Pitch("E,",painter); //Another error ,-,
72           pitches[7] = pitchLowE; //Correct
73           Pitch pitchHighE = new Pitch("e",painter); //Correct
74           pitches[8] = pitchHighE; //Correct
75   
76           return pitches; //Correct
77       } //hmm kinda working now abc notation is a weird name for spacing!!!
78   
79       private static Pitch find(String token, Pitch[] pitches) throws Exception {
80           for (int i = 0; i < pitches.length; i = i + 1) {
81               Pitch pitch = pitches[i];
82               if (pitch.abcName().equals(token) ) {
83                   return pitch;
84               }
85           }
86           throw new Exception("### PITCH" + token + " NOT FOUND");
87       }
88       private static void display(Pitch[] pitches) {
89           for (int i = 0; i < pitches.length; i = i + 1) {
90               System.out.println(pitches[i].toString());
91           }
92       }
93       private static void playMelody(String input, Pitch[] pitches) throws Exception {
94           Scanner scan = new Scanner(input);
95           while (scan.hasNext()) {
96               String token = scan.next();
97               Pitch pitch = find(token,pitches);
98               pitch.play("1");
99           }
100      }
101      static private void showErrorMessage(String message) {
102          leo.setVisible(false);
103          JOptionPane.showMessageDialog(null,message);
104      }
105      private static void initialization() {
106          leo = new SPainter("Chromesthesia",500,500);
107          leo.setVisible(false);
108          leo.setBrushWidth(7);
109          pitches = establishPitches(leo);
110          display(pitches);
111      }
112      private static String getInput() {
113          leo.setVisible(false);
114          String label = "Please enter a melody in abc notation, or EXIT...";
115          String input = JOptionPane.showInputDialog(null,label);
116          leo.setVisible(true);
117          if (input == null) {
118              input = "";
119          }
120          return input;
121      }
122  
123      static private void cleanup() {
124          System.exit(0);
125      }
126  }
127