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    
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[9];
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           return pitches;
78       }
79   
80       private static Pitch find(String token, Pitch[] pitches) throws Exception {
81           for ( int i = 0; i < pitches.length; i = i + 1) {
82               Pitch pitch = pitches[i];
83               if ( pitch.abcName().equals(token)){
84                   return pitch;
85               }
86           }
87           throw new Exception("### PITCH" + token + "NOT FOUND");
88       }
89   
90       private static void display(Pitch[] pitches) {
91           for ( int i = 0; i < pitches.length; i = i + 1 ) {
92               System.out.println(pitches[i].toString());
93           }
94       }
95   
96       private static void playMelody(String input, Pitch[] pitches) throws Exception {
97           Scanner scanner = new Scanner(input);
98           while ( scanner.hasNext()) {
99               String token = scanner.next();
100              Pitch pitch = find(token, pitches);
101              pitch.play("1");
102          }
103      }
104  
105      //  INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
106  
107      static private void showErrorMessage (String message) {
108          miro.setVisible(false);
109          JOptionPane.showMessageDialog(null, message);
110      }
111  
112      private static void initializaiton() {
113          // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
114          miro = new SPainter("Chromesthesia", 500,500);
115          miro.setVisible(false);
116          miro.setBrushWidth(7);
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  }