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