Chromesthesia.java
/** This program interprets melodic lines given in ABC notation as a* chromesthete might.*
 * A Pitch class will be defined, and will take center stage in the* processing.*
 ** Interpreting a melody in ABC notation will amount to flashing* colored rectangles for prescribed durations,
 * while sounding* the pitch! The color of the rectangle will correspond to pitch* class. The duration will correspond to the duration of the note.
 */

//for first version of program the duration will be held at one constant beat
//three sorts of images will appear on the screen:
//chromesthetic output box, text input box, and error message box. only one will appear on the screen at a  time

//ABC represents notes in a manner consistent with these examples: C, D, E, C D E c d e
//google ABC music representation if i want to know more about it

package chromesthesia1;

import painter.SPainter;

import javax.swing.*;
import java.util.Scanner;

public class Chromesthesia {

    //infrastructure for the program -- launching a graphics thread

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new ThreadForGUI());
    }

    private static class ThreadForGUI implements Runnable{
        @Override
        public void run() {
            new Chromesthesia();
        }
    }

    public Chromesthesia(){
        interpreter();
    }

    //featured variables
    private static SPainter miro;
    private static Pitch[] pitches;

    //the INTERPreter
    public static void interpreter() {
        initialization(); //miro and pitches

        while (true ) {
            String input = getInput();
            if (input.equalsIgnoreCase("EXIT")) {
                break;
            } else {
                try {
                    playMelody(input, pitches);
                } catch (Exception ex) {
                    showErrorMessage(ex.toString());
                }

            }
        }

        cleanup(); //miro has to goj
    }

    //methods pertaining to the chromesthetic pitches

    private static Pitch[] establishPitches(SPainter painter) {
        Pitch[] pitches = new Pitch[21];
        Pitch pitchMiddleC = new Pitch("C", painter);
        pitches[0] = pitchMiddleC;
        Pitch pitchLowC = new Pitch("C,", painter);
        pitches[1] = pitchLowC;
        Pitch pitchHighC = new Pitch("c", painter);
        pitches[2] = pitchHighC;
        Pitch pitchMiddleD = new Pitch("D", painter);
        pitches[3] = pitchMiddleD;
        Pitch pitchLowD = new Pitch("D,", painter);
        pitches[4] = pitchLowD;
        Pitch pitchHighD = new Pitch("d", painter);
        pitches[5] = pitchHighD;
        Pitch pitchMiddleE = new Pitch("E", painter);
        pitches[6] = pitchMiddleE;
        Pitch pitchLowE = new Pitch("E,", painter);
        pitches[7] = pitchLowE;
        Pitch pitchHighE = new Pitch("e", painter);
        pitches[8] = pitchHighE;
        //new pitches
        Pitch pitchMiddleA = new Pitch("A", painter);
        pitches[9] = pitchMiddleA;
        Pitch pitchLowA = new Pitch("A,", painter);
        pitches[10] = pitchLowA;
        Pitch pitchHighA = new Pitch("a", painter);
        pitches[11] = pitchHighA;
        Pitch pitchMiddleB = new Pitch("B", painter);
        pitches[12] = pitchMiddleB;
        Pitch pitchLowB = new Pitch("B,", painter);
        pitches[13] = pitchLowB;
        Pitch pitchHighB = new Pitch("b", painter);
        pitches[14] = pitchHighB;
        Pitch pitchMiddleF = new Pitch("F", painter);
        pitches[15] = pitchMiddleF;
        Pitch pitchLowF = new Pitch("F,", painter);
        pitches[16] = pitchLowF;
        Pitch pitchHighF = new Pitch("f", painter);
        pitches[17] = pitchHighF;
        Pitch pitchMiddleG = new Pitch("G", painter);
        pitches[18] = pitchMiddleG;
        Pitch pitchLowG = new Pitch("G,", painter);
        pitches[19] = pitchLowG;
        Pitch pitchHighG = new Pitch("g", painter);
        pitches[20] = pitchHighG;

        return pitches;

    }

    private static Pitch find(String token, Pitch[] pitches) throws Exception {
        for (int i = 0; i < pitches.length; i = i + 1) {
            Pitch pitch = pitches[i];
            if (pitch.abcName().equals(token)) {
                return pitch;
            }
        }//e
        throw new Exception("### PITCH " + token + " NOT FOUND");
    }

    private static void display(Pitch[] pitches){
        for (int i = 0; i < pitches.length; i = i + 1) {
            System.out.println(pitches[i].toString());
        }
    }

    private static void playMelody(String input, Pitch[] pitches) throws Exception {
        Scanner scanner = new Scanner(input);
        while (scanner.hasNext()){
            String token = scanner.next();
            Pitch pitch = find(token, pitches);
            pitch.play("1");
        }

    }

    //initialization, cleanup, getting input, error messaging

    static private void showErrorMessage(String message) {
        miro.setVisible(false);
        JOptionPane.showMessageDialog(null, message);
    }

    private static void initialization() {
        //establish the painter and give it a substantial brush widthA
        miro = new SPainter("Chromesthesia 1", 500, 500);
        miro.setVisible(false);
        miro.setBrushWidth(7);
        //establish the chromestitic pitch class objects
        pitches = establishPitches(miro);
        display(pitches);
    }

    public static String getInput() {
        miro.setVisible(false);
        String label = "Please enter a melody in ABC notation, or EXIT ...     ";
        String input = JOptionPane.showInputDialog(null, label);
        miro.setVisible(true);
        if (input == null) {input = "";}
        return input;
    }

    static private void cleanup(){
        System.exit(0);
    }

}