Chromes1.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 Chromes1 {
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 Chromes1();
16           }
17       }
18       public Chromes1() {
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[9];
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           return pitches;
64       }
65       private static Pitch find(String token, Pitch[] pitches) throws Exception {
66           for ( int i = 0; i < pitches.length; i = i + 1 ) {
67               Pitch pitch = pitches[i];
68               if ( pitch.abcName().equals(token) ) {
69                   return pitch;
70               }
71           }
72           throw new Exception("### PITCH " + token + " NOT FOUND");
73       }
74       private static void display(Pitch[] pitches) {
75           for ( int i = 0; i < pitches.length; i = i + 1 ) {
76               System.out.println(pitches[i].toString());
77           }
78       }
79       private static void playMelody(String input, Pitch[] pitches) throws Exception {
80   
81           Scanner scanner = new Scanner(input);
82           while ( scanner.hasNext() ) {
83               String token = scanner.next();
84               Pitch pitch = find(token,pitches);
85               pitch.play("1");
86           }
87       }
88       // INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING
89       static private void showErrorMessage(String message) {
90           miro.setVisible(false);
91           JOptionPane.showMessageDialog(null, message);
92       }
93       private static void initialization() {
94   // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH
95           miro = new SPainter("Chromesthesia",500,500);
96           miro.setVisible(false);
97           miro.setBrushWidth(7);
98   // ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS
99           pitches = establishPitches(miro);
100          display(pitches);
101      }
102      private static String getInput() {
103          miro.setVisible(false);
104          String label = "Please enter a melody in ABC notation, or EXIT ... ";
105          String input = JOptionPane.showInputDialog(null,label);
106          miro.setVisible(true);
107          if ( input == null ) { input = ""; }
108          return input;
109      }
110      static private void cleanup() {
111          System.exit(0);
112      }
113  }
114