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