/home/sjenks/NetBeansProjects/CS1/src/chromesthesia0/Chromesthesia.java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package chromesthesia0;
23
24 import java.util.Scanner;
25 import javax.swing.JOptionPane;
26 import javax.swing.SwingUtilities;
27 import painter.SPainter;
28
29
30
31
32 @author
33
34 public class Chromesthesia {
35
36
37
38
39 @param args
40
41 public static void main(String[] args) {
42 SwingUtilities.invokeLater(new ThreadForGUI());
43
44 }
45
46
47
48
49 private static class ThreadForGUI implements Runnable {
50 @Override
51 public void run() {
52 new Chromesthesia();
53 }
54 }
55
56 public Chromesthesia(){
57 interpreter();
58 }
59
60
61
62 private static SPainter miro;
63 public static Pitch[] pitches;
64
65
66
67 public static void interpreter(){
68 initialization();
69
70 while ( true ) {
71 String input = getInput();
72 if ( input.equalsIgnoreCase("EXIT")){
73 break;
74 } else {
75 try {
76 playMelody(input,pitches);
77 }catch (Exception ex){
78 showErrorMessage(ex.toString());
79
80 }
81 }
82 }
83
84 cleanup();
85
86 }
87
88
89
90 private static Pitch[] establishPitches (SPainter painter){
91 Pitch[] pitches = new Pitch [9];
92 Pitch pitchMiddleC = new Pitch ("C" , painter);
93 pitches [0] = pitchMiddleC;
94 Pitch pitchLowC = new Pitch ("C," , painter);
95 pitches [1] = pitchLowC;
96 Pitch pitchHighC = new Pitch ("c" , painter);
97 pitches [2] = pitchHighC;
98 Pitch pitchMiddleD = new Pitch ("D" , painter);
99 pitches [3] = pitchMiddleD;
100 Pitch pitchLowD = new Pitch ("D," , painter);
101 pitches [4] = pitchLowD;
102 Pitch pitchHighD = new Pitch ("d" , painter);
103 pitches [5] = pitchHighD;
104 Pitch pitchMiddleE = new Pitch ("E" , painter);
105 pitches [6] = pitchMiddleE;
106 Pitch pitchLowE = new Pitch ("E," , painter);
107 pitches [7] = pitchLowE;
108 Pitch pitchHighE = new Pitch ("e" , painter);
109 pitches [8] = pitchHighE;
110 return pitches;
111 }
112 private static Pitch find (String token, Pitch[] pitches) throws Exception {
113 for ( int i = 0; i < pitches.length; i = i + 1 ){
114 Pitch pitch = pitches[i];
115 if ( pitch.abcName().equals(token)){
116 return pitch;
117 }
118 }
119 throw new Exception ("### PITCH" + token + "NOT FOUND");
120 }
121
122 private static void display (Pitch[] pitches){
123 for ( int i = 0; i < pitches.length; i = i+1){
124 System.out.println(pitches[i].toString());
125 }
126 }
127 private static void playMelody(String input, Pitch[] pitches) throws Exception {
128 Scanner scanner = new Scanner(input);
129 miro.pause();
130 while ( scanner.hasNext()){
131 String token = scanner.next();
132 Pitch pitch = find (token,pitches);
133 pitch.play ("1");
134 }
135 }
136
137
138 private static void showErrorMessage(String message) {
139 miro.setVisible (false);
140 JOptionPane.showMessageDialog(null, message);
141 }
142
143 private static void initialization() {
144
145 miro = new SPainter ("Chromesthesia", 500,500);
146 miro.setVisible (false);
147 miro.setBrushWidth(7);
148
149 pitches = establishPitches(miro);
150 display (pitches);
151 }
152 private static String getInput() {
153 miro.setVisible (false);
154 String label = "Please enter a melody in ABC notation, or EXIT... ";
155 String input = JOptionPane.showInputDialog(null,label);
156 miro.setVisible(true);
157 if (input == null ) {input = "";}
158 return input;
159 }
160 static private void cleanup(){
161 System.exit(0);
162
163 }
164
165 }
166