Chromesthesia.java
1    package chromesthesia1;
2    import painter.SPainter;
3    
4    import javax.swing.*;
5    import java.util.Scanner;
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           }
37           cleanup(); // miro has to go
38       }
39       // METHODS PERTAINING TO THE CHROMESTHETIC PITCHES
40       private static Pitch[] establishPitches(SPainter painter) {
41           Pitch[] pitches = new Pitch[21];
42           Pitch pitchMiddleC = new Pitch("C",painter);
43           pitches[0] = pitchMiddleC;
44           Pitch pitchLowC = new Pitch("C,",painter);
45           pitches[1] = pitchLowC;
46           Pitch pitchHighC = new Pitch("c",painter);
47           pitches[2] = pitchHighC;
48           Pitch pitchMiddleD = new Pitch("D",painter);
49           pitches[3] = pitchMiddleD;
50           Pitch pitchLowD = new Pitch("D,",painter);
51           pitches[4] = pitchLowD;
52           Pitch pitchHighD = new Pitch("d",painter);
53           pitches[5] = pitchHighD;
54           Pitch pitchMiddleE = new Pitch("E",painter);
55           pitches[6] = pitchMiddleE;
56           Pitch pitchLowE = new Pitch("E,",painter);
57           pitches[7] = pitchLowE;
58           Pitch pitchHighE = new Pitch("e",painter);
59           pitches[8] = pitchHighE;
60           // extending for F
61           Pitch pitchLowF = new Pitch("F",painter);
62           pitches[9] = pitchLowF;
63           Pitch pitchMidF = new Pitch("F,",painter);
64           pitches[10] = pitchMidF;
65           Pitch pitchHighF = new Pitch("f",painter);
66           pitches[11] = pitchHighF;
67           // G
68           Pitch pitchLowG = new Pitch("G",painter);
69           pitches[12] = pitchLowG;
70           Pitch pitchMidG = new Pitch("G,",painter);
71           pitches[13] = pitchMidG;
72           Pitch pitchHighG = new Pitch("g",painter);
73           pitches[14] = pitchHighG;
74           // A
75           Pitch pitchLowA = new Pitch("A",painter);
76           pitches[15] = pitchLowA;
77           Pitch pitchMidA = new Pitch("A,",painter);
78           pitches[16] = pitchMidA;
79           Pitch pitchHighA = new Pitch("a",painter);
80           pitches[17] = pitchHighA;
81           // B
82           Pitch pitchLowB = new Pitch("B",painter);
83           pitches[18] = pitchLowB;
84           Pitch pitchMidB = new Pitch("B,",painter);
85           pitches[19] = pitchMidB;
86           Pitch pitchHighB = new Pitch("b",painter);
87           pitches[20] = pitchHighB;
88           return pitches;
89       }
90       private static Pitch find(String token, Pitch[] pitches) throws Exception {
91           for ( int i = 0; i < pitches.length; i = i + 1 ) {
92               Pitch pitch = pitches[i];
93               if ( pitch.abcName().equals(token) ) {
94                   return pitch;
95               }
96           }
97           throw new Exception("### PITCH " + token + " NOT FOUND");
98       }
99       private static void display(Pitch[] pitches) {
100          for (int i = 0; i < pitches.length; i = i + 1) {
101              System.out.println(pitches[i].toString());
102          }
103      }
104      private static void playMelody(String input, Pitch[] pitches) throws Exception {
105          Scanner scanner = new Scanner(input);
106          while ( scanner.hasNext() ) {
107              String token = scanner.next();
108              Pitch pitch = find(token,pitches);
109              pitch.play("1");
110          } }
111      // INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
112      static private void showErrorMessage(String message) {
113          miro.setVisible(false);
114          JOptionPane.showMessageDialog(null, message);
115      }
116      private static void initialization() {
117          // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
118          miro = new SPainter("Chromesthesia",500,500);
119          miro.setVisible(false);
120          miro.setBrushWidth(7);
121          // ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS
122          pitches = establishPitches(miro);
123          display(pitches);
124      }
125      private static String getInput() {
126          miro.setVisible(false);
127          String label = "Please enter a melody in ABC notation, or EXIT ...     ";
128          String input = JOptionPane.showInputDialog(null,label);
129          miro.setVisible(true);
130          if ( input == null ) { input = ""; }
131          return input;
132      }
133      static private void cleanup() {
134          System.exit(0);
135      } }
136