Chromesthesia.java
1    package chromesthesia0;
2    import java.util.Scanner;
3    import javax.swing.JOptionPane;
4    import javax.swing.SwingUtilities;
5    import painter.SPainter;
6    public class Chromesthesia {
7        // INFRASTRUCTURE FOR THE PROGRAM -- LAUNCHING A "GRAPHICS" THREAD
8        public static void main(String[] args) {
9            SwingUtilities.invokeLater(new ThreadForGUI());
10       }
11       private static class ThreadForGUI implements Runnable {
12           @Override
13           public void run() {
14               new Chromesthesia();
15           } }
16       public Chromesthesia() {
17           interpreter();
18       }
19       // FEATURED VARIABLES
20       private static SPainter miro;
21       private static Pitch[] pitches;
22       // THE INTERPRETER
23       public static void interpreter() {
24           initialization(); // miro and pitches
25           while ( true ) {
26               String input = getInput();
27               if ( input.equalsIgnoreCase("EXIT") ) {
28                   break;
29               } else {
30                   try {
31                       playMelody(input,pitches);
32                   } catch (Exception ex) {
33                       showErrorMessage(ex.toString());
34                   } }
35           }
36           cleanup(); // miro has to go
37       }
38       // METHODS PERTAINING TO THE CHROMESTHETIC PITCHES
39       private static Pitch[] establishPitches(SPainter painter) {
40           Pitch[] pitches = new Pitch[9];
41           Pitch pitchMiddleC = new Pitch("C",painter);
42           pitches[0] = pitchMiddleC;
43           Pitch pitchLowC = new Pitch("C,",painter);
44           pitches[1] = pitchLowC;
45           Pitch pitchHighC = new Pitch("c",painter);
46           pitches[2] = pitchHighC;
47           Pitch pitchMiddleD = new Pitch("D",painter);
48           pitches[3] = pitchMiddleD;
49           Pitch pitchLowD = new Pitch("D,",painter);
50           pitches[4] = pitchLowD;
51           Pitch pitchHighD = new Pitch("d",painter);
52           pitches[5] = pitchHighD;
53           Pitch pitchMiddleE = new Pitch("E",painter);
54           pitches[6] = pitchMiddleE;
55           Pitch pitchLowE = new Pitch("E,",painter);
56           pitches[7] = pitchLowE;
57           Pitch pitchHighE = new Pitch("e",painter);
58           pitches[8] = pitchHighE;
59           return pitches;
60       }
61       private static Pitch find(String token, Pitch[] pitches) throws Exception {
62           for ( int i = 0; i < pitches.length; i = i + 1 ) {
63               Pitch pitch = pitches[i];
64               if ( pitch.abcName().equals(token) ) {
65                   return pitch;
66               }
67           }
68           throw new Exception("### PITCH " + token + " NOT FOUND");
69       }
70       private static void display(Pitch[] pitches) {
71           for ( int i = 0; i < pitches.length; i = i + 1 ) {
72               System.out.println(pitches[i].toString());
73           }
74       }
75       private static void playMelody(String input, Pitch[] pitches) throws Exception {
76   
77           Scanner scanner = new Scanner(input);
78           while ( scanner.hasNext() ) {
79               String token = scanner.next();
80               Pitch pitch = find(token,pitches);
81               pitch.play("1");
82           } }
83       // INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
84       static private void showErrorMessage(String message) {
85           miro.setVisible(false);
86           JOptionPane.showMessageDialog(null, message);
87       }
88       private static void initialization() {
89           // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
90           miro = new SPainter("Chromesthesia",500,500);
91           miro.setVisible(false);
92           miro.setBrushWidth(7);
93           // ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS
94           pitches = establishPitches(miro);
95           display(pitches);
96       }
97       private static String getInput() {
98           miro.setVisible(false);
99           String label = "Please enter a melody in ABC notation, or EXIT ...     ";
100          String input = JOptionPane.showInputDialog(null,label);
101          miro.setVisible(true);
102          if ( input == null ) { input = ""; }
103          return input;
104      }
105      static private void cleanup() {
106          System.exit(0);
107      }
108  }