1 /* 2 * 3 */ 4 package chromesthsia0; 5 6 import painter.SPainter; 7 8 import javax.swing.*; 9 import java.util.Scanner; 10 11 public class Chromesthesia { 12 //infrastructure for the program -- launching a graphics thread 13 public static void main(String[] args) { 14 SwingUtilities.invokeLater(new ThreadForGUI()); 15 } 16 private static class ThreadForGUI implements Runnable { 17 @Override 18 public void run() { 19 new Chromesthesia(); 20 } 21 } 22 public Chromesthesia() { 23 interpreter(); 24 } 25 //featured variables 26 private static SPainter miro; 27 private static Pitch[] pitches; 28 29 //the interpreter 30 public static void interpreter() { 31 initialization(); //miro and pitches 32 while(true) { 33 String input = getInput(); 34 if (input.equalsIgnoreCase("EXIT")) { 35 break; 36 } else { 37 try { 38 playMelody(input,pitches); 39 } catch (Exception ex) { 40 showErrorMessage(ex.toString()); 41 } 42 } 43 } 44 cleanup(); //miro has to go 45 } 46 //methods pretaining to the chromesthetic pitches 47 48 private static Pitch[] establishPitches(SPainter painter) { 49 Pitch[] pitches = new Pitch[9]; 50 Pitch pitchMiddleC = new Pitch("C",painter); 51 pitches[0] = pitchMiddleC; 52 Pitch pitchLowC = new Pitch("C,",painter); 53 pitches[1] = pitchLowC; 54 Pitch pitchHighC = new Pitch("c",painter); 55 pitches[2] = pitchHighC; 56 Pitch pitchMiddleD = new Pitch("D",painter); 57 pitches[3] = pitchMiddleD; 58 Pitch pitchLowD = new Pitch("D,",painter); 59 pitches[4] = pitchLowD; 60 Pitch pitchHighD = new Pitch("d",painter); 61 pitches[5] = pitchHighD; 62 Pitch pitchMiddleE = new Pitch("E",painter); 63 pitches[6] = pitchMiddleE; 64 Pitch pitchLowE = new Pitch("E,",painter); 65 pitches[7] = pitchLowE; 66 Pitch pitchHighE = new Pitch("e",painter); 67 pitches[8] = pitchHighE; 68 return pitches; 69 } 70 private static Pitch find(String token, Pitch[] pitches) throws Exception { 71 for (int i = 0; i < pitches.length; i = i + 1) { 72 Pitch pitch = pitches[i]; 73 if (pitch.abcName().equals(token)) { 74 return pitch; 75 } 76 } 77 throw new Exception("### PITCH " + token + " NOT FOUND"); 78 } 79 private static void display(Pitch[] pitches) { 80 for (int i = 0; i < pitches.length; i = i + 1) { 81 System.out.println(pitches[i].toString()); 82 } 83 } 84 private static void playMelody(String input, Pitch[] pitches) throws Exception { 85 Scanner scanner = new Scanner(input); 86 while (scanner.hasNext()) { 87 String token = scanner.next(); 88 Pitch pitch = find(token,pitches); 89 pitch.play("1"); 90 } 91 } 92 //initialization, cleanup, getting input, error messaging 93 static private void showErrorMessage(String message) { 94 miro.setVisible(false); 95 JOptionPane.showMessageDialog(null,message); 96 } 97 private static void initialization() { 98 //establish the painter and give it a substantial brush width 99 miro = new SPainter("Chromesthesia",500,500); 100 miro.setVisible(false); 101 miro.setBrushWidth(7); 102 //establish the chromesthatic pitch class objects 103 pitches = establishPitches(miro); 104 display(pitches); 105 } 106 private static String getInput() { 107 miro.setVisible(false); 108 String label = "Please enter a melody in ABC notation, or EXIT ... "; 109 String input = JOptionPane.showInputDialog(null,label); 110 miro.setVisible(true); 111 if (input==null) {input="";} 112 return input; 113 } 114 static private void cleanup() { 115 System.exit(0); 116 } 117 } 118