Chromesthesia.java
1    package chromesthesia0;
2    
3    import painter.SPainter;
4    
5    import javax.swing.*;
6    import java.util.Scanner;
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       private static SPainter miro;
29       private static Pitch[] pitches;
30   
31       //THE INTERPRETER
32   
33       public static void interpreter(){
34   
35           initialization(); //miro and pitches
36   
37           while (true) {
38               String input = getInput();
39               if(input.equalsIgnoreCase("EXIT")) {
40                   break;
41               }
42               else{
43                   try{
44                       playMelody(input, pitches);
45                   }
46                   catch (Exception ex) {
47                       showErrorMessage(ex.toString());
48                   }
49               }
50           }
51           cleanup(); //miro has to go
52       }
53   
54       //METHODS PERTAINING TO THE CHROMESTHETIC PITCHES
55   
56       private static Pitch[] establishPitches(SPainter painter) {
57           Pitch[] pitches = new Pitch[9];
58           Pitch pitchMiddleC = new Pitch("C", painter);
59           pitches[0] = pitchMiddleC;
60           Pitch pitchLowC = new Pitch("C,", painter);
61           pitches[1] = pitchLowC;
62           Pitch pitchHighC = new Pitch("c", painter);
63           pitches[2] = pitchHighC;
64           Pitch pitchMiddleD = new Pitch("D", painter);
65           pitches[3] = pitchMiddleD;
66           Pitch pitchLowD = new Pitch("D,", painter);
67           pitches[4] = pitchLowD;
68           Pitch pitchHighD = new Pitch("d", painter);
69           pitches[5] = pitchHighD;
70           Pitch pitchMiddleE = new Pitch("E", painter);
71           pitches[6] = pitchMiddleE;
72           Pitch pitchLowE = new Pitch("E,", painter);
73           pitches[7] = pitchLowE;
74           Pitch pitchHighE = new Pitch("e", painter);
75           pitches[8] = pitchHighE;
76           return pitches;
77       }
78   
79       private static Pitch find(String token, Pitch[] pitches) throws Exception {
80           for (int i = 0; i < pitches.length; i++) {
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   
89       private static void display(Pitch[] pitches) {
90           for (int i = 0; i < pitches.length; i++){
91               System.out.println(pitches[i].toString());
92           }
93       }
94   
95       private static void playMelody(String input, Pitch[] pitches) throws Exception {
96           Scanner scanner = new Scanner(input);
97           while (scanner.hasNext()) {
98               String token = scanner.next();
99               Pitch pitch = find(token,pitches);
100              pitch.play("1");
101          }
102      }
103  
104      //INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
105  
106      static private void showErrorMessage(String message) {
107          miro.setVisible(false);
108          JOptionPane.showInputDialog(null, message);
109      }
110  
111      static private void initialization() {
112          //ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
113          miro = new SPainter("Chromesthesia", 500,500);
114          miro.setVisible(false);
115          miro.setBrushWidth(7);
116  
117          //ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS
118          pitches = establishPitches(miro);
119          display(pitches);
120      }
121  
122      private static String getInput() {
123          miro.setVisible(false);
124          String label = "Please enter a melody in ABC notation, or EXIT...";
125          String input = JOptionPane.showInputDialog(null,label);
126          miro.setVisible(true);
127          if (input == null) {input = "";}
128          return input;
129      }
130  
131      static private void cleanup() {
132          System.exit(0);
133      }
134  }