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 Chromes2 { 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 Chromes2(); 16 } 17 } 18 public Chromes2() { 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[21]; 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 Pitch pitchMiddleA = new Pitch("A",painter); 64 pitches[9] = pitchMiddleA; 65 Pitch pitchLowA = new Pitch("A,",painter); 66 pitches[10] = pitchLowA; 67 Pitch pitchHighA = new Pitch("a",painter); 68 pitches[11] = pitchHighA; 69 Pitch pitchMiddleF = new Pitch("F",painter); 70 pitches[12] = pitchMiddleF; 71 Pitch pitchLowF = new Pitch("F,",painter); 72 pitches[13] = pitchLowF; 73 Pitch pitchHighF = new Pitch("f",painter); 74 pitches[14] = pitchHighF; 75 Pitch pitchMiddleG = new Pitch("G",painter); 76 pitches[15] = pitchMiddleG; 77 Pitch pitchLowG = new Pitch("G,",painter); 78 pitches[16] = pitchLowG; 79 Pitch pitchHighG = new Pitch("g",painter); 80 pitches[17] = pitchHighG; 81 Pitch pitchMiddleB = new Pitch("B",painter); 82 pitches[18] = pitchMiddleB; 83 Pitch pitchLowB = new Pitch("B,",painter); 84 pitches[19] = pitchLowB; 85 Pitch pitchHighB = new Pitch("b",painter); 86 pitches[20] = pitchHighB; 87 return pitches; 88 } 89 private static Pitch find(String token, Pitch[] pitches) throws Exception { 90 for ( int i = 0; i < pitches.length; i = i + 1 ) { 91 Pitch pitch = pitches[i]; 92 if ( pitch.abcName().equals(token) ) { 93 return pitch; 94 } 95 } 96 throw new Exception("### PITCH " + token + " NOT FOUND"); 97 } 98 private static void display(Pitch[] pitches) { 99 for ( int i = 0; i < pitches.length; i = i + 1 ) { 100 System.out.println(pitches[i].toString()); 101 } 102 } 103 private static void playMelody(String input, Pitch[] pitches) throws Exception { 104 105 Scanner scanner = new Scanner(input); 106 while ( scanner.hasNext() ) { 107 String token = scanner.next(); 108 Pitch pitch = find(token,pitches); 109 pitch.play("1"); 110 } 111 } 112 // INITIALIZATION, CLEANUP, GETTING INPUT, ERROR MESSAGING 113 static private void showErrorMessage(String message) { 114 miro.setVisible(false); 115 JOptionPane.showMessageDialog(null, message); 116 } 117 private static void initialization() { 118 // ESTABLISH THE PAINTER AND GIVE IT A SUBSTANTIAL BRUSH WIDTH 119 miro = new SPainter("Chromesthesia",500,500); 120 miro.setVisible(false); 121 miro.setBrushWidth(7); 122 // ESTABLISH THE CHROMESTITIC PITCH CLASS OBJECTS 123 pitches = establishPitches(miro); 124 display(pitches); 125 } 126 private static String getInput() { 127 miro.setVisible(false); 128 String label = "Please enter a melody in ABC notation, or EXIT ... "; 129 String input = JOptionPane.showInputDialog(null,label); 130 miro.setVisible(true); 131 if ( input == null ) { input = ""; } 132 return input; 133 } 134 static private void cleanup() { 135 System.exit(0); 136 } 137 } 138