/home/akc/NetBeansProjects/CS1/src/chromesthesia2/Pitch.java
  1 /*
  2  * The Pitch class models the pitch of a note in a manner that will facilitate
  3  * the chromesthetic processing of the pitch. A Pitch object will have five
  4  * properties:
  5  * - String name | ABC notation pitch name
  6  * - SPainter painter | the painting agent
  7  * - Note note | a note that will be set to the pitch corresponding to the
  8  *   ABC notation pitch name
  9  * - SRectangle box | an SRectangle object that will chromesthetically
 10  *   represent the pitch
 11  * - Color color | the color associated with the pitch for the presumed
 12  *   chromesthete
 13  */
 14 package chromesthesia2;
 15 
 16 import java.awt.Color;
 17 import note.SNote;
 18 import painter.SPainter;
 19 import shapes.SRectangle;
 20 
 21 /**
 22  *
 23  * @author akc
 24  */
 25 public class Pitch {
 26     
 27     // INSTANCE VARIABLES
 28     private String abcName;
 29     private SPainter painter;
 30     private SRectangle box;
 31     private SNote note;
 32     private Color color;
 33     
 34     public Pitch(String abcName, SPainter painter) {
 35         this.abcName = abcName;
 36         this.painter = painter;
 37         this.box = new SRectangle(painter.painterHeight - 50, painter.painterWidth - 50);
 38         this.note = createNoteForThisPitch(abcName);
 39         this.color = getPitchClassColor(abcName.substring(0, 1).toUpperCase());
 40     }
 41     
 42     public String toString() {
 43         return "[ " + abcName + "|" + note.degree()+ "|" + color + " ]";
 44     }
 45     
 46     public String abcName() {
 47         return abcName;
 48     }
 49     
 50     private SNote createNoteForThisPitch(String abcPitchClassName) {
 51         SNote note = new SNote();
 52         if (abcPitchClassName.equals("C")) {
 53             // nothing to do
 54         } else if (abcPitchClassName.equals("C,")) {
 55             note.lp(7);
 56         } else if (abcPitchClassName.equals("c")) {
 57             note.rp(7);
 58         } else if (abcPitchClassName.equals("D")) {
 59             note.rp(1);
 60         } else if (abcPitchClassName.equals("D,")) {
 61             note.lp(6);
 62         } else if (abcPitchClassName.equals("d")) {
 63             note.rp(8);
 64         } else if (abcPitchClassName.equals("E")) {
 65             note.rp(2);
 66         } else if (abcPitchClassName.equals("E,")) {
 67             note.lp(5);
 68         } else if (abcPitchClassName.equals("e")) {
 69             note.rp(9);
 70         } else if (abcPitchClassName.equals("F")) {
 71             note.rp(3);
 72         } else if (abcPitchClassName.equals("F,")) {
 73             note.lp(4);
 74         } else if (abcPitchClassName.equals("f")) {
 75             note.rp(10);
 76         } else if (abcPitchClassName.equals("G")) {
 77             note.rp(4);
 78         } else if (abcPitchClassName.equals("G,")) {
 79             note.lp(3);
 80         } else if (abcPitchClassName.equals("g")) {
 81             note.rp(11);
 82         } else if (abcPitchClassName.equals("A")) {
 83             note.rp(5);
 84         } else if (abcPitchClassName.equals("A,")) {
 85             note.lp(2);
 86         } else if (abcPitchClassName.equals("a")) {
 87             note.rp(12);
 88         } else if (abcPitchClassName.equals("B")) {
 89             note.rp(6);
 90         } else if (abcPitchClassName.equals("B,")) {
 91             note.lp(1);
 92         } else if (abcPitchClassName.equals("b")) {
 93             note.rp(13);
 94         }
 95         return note;
 96     }
 97     
 98     private Color getPitchClassColor(String letter) {
 99         if (letter.equals("C")) {
100             return new Color(127, 0, 127);
101         }else if(letter.equals("D")) {
102             return new Color(255, 255, 0);
103         } else if(letter.equals("E")) {
104             return new Color(255, 0, 0);
105         } else if(letter.equals("F")) {
106             return new Color(255, 127, 0);
107         } else if(letter.equals("G")) {
108             return new Color(0, 255, 255);
109         } else if(letter.equals("A")) {
110             return new Color(0, 0, 255);
111         } else if(letter.equals("B")) {
112             return new Color(0, 255, 0);
113         } else {
114             return Color.BLACK;
115         }
116     }
117     
118     public void play(String d) {
119         painter.setColor(color);
120         painter.paint(box);
121         painter.setColor(randomColor());
122         painter.draw(box);
123         if (d.equals("1")) {
124             note.play();
125         } else if (d.equals("2")) {
126             note.x2();
127             note.play();
128             note.s2();
129         } else if (d.equals("1/2")) {
130             note.s2();
131             note.play();
132             note.x2();
133         } else if (d.equals("3")) {
134             note.x3();
135             note.play();
136             note.s3();
137         } else if (d.equals("1/3")) {
138             note.s3();
139             note.play();
140             note.x3();
141         } else if (d.equals("2/3")) {
142             note.s3();
143             note.x2();
144             note.play();
145             note.s2();
146             note.x3();
147         }
148     }
149     
150     private static Color randomColor() {
151         int rv = (int) (Math.random() * 256);
152         int gv = (int) (Math.random() * 256);
153         int bv = (int) (Math.random() * 256);
154         return new Color(rv, gv, bv);
155     }
156 }
157