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