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