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 properties 
4     * - String name | ABC notation of pitch name 
5     * - SPainter painter | The painting agent 
6     * - Note note | A note that will be set to the pitch corresponding to the 
7     *   ABC notation of the pitch name 
8     * - SRectangle box | an SRectangle object that will chromesthetically 
9     *   represent the pitch 
10    * - Color color | the color associated with the pitch for the presumed 
11    *   chromesthete 
12    */
13   
14   package chromesthesia0;
15   
16   import note.SNote;
17   import painter.SPainter;
18   import shapes.SRectangle;
19   import java.awt.*;
20   
21   public class Pitch {
22       //instance variables
23       private String abcName;
24       private SPainter painter;
25       private SRectangle box;
26       private SNote note;
27       private Color color;
28   
29       //constructor
30       public Pitch(String abcName, SPainter painter) {
31           this.abcName = abcName;
32           this.painter = painter;
33           this.box = new SRectangle(painter.painterHeight-50,painter.painterWidth-50);
34           this.note = createNoteForThisPitch(abcName);
35           this.color = getPitchClassColor(abcName.substring(0,1).toUpperCase());
36       }
37   
38       //toString() method
39       public String toString(){
40           return"[ " + abcName + " | " + note.degree() + " | " + color + " ]";
41       }
42   
43       //abcName() method
44       public String abcName(){
45           return abcName;
46       }
47       private SNote createNoteForThisPitch(String abcPitchClassName) {
48           SNote note = new SNote();
49           if (abcPitchClassName.equals("C")) {// nothing to do
50           } else if (abcPitchClassName.equals("C,")) {
51               note.lp(7);
52           } else if (abcPitchClassName.equals("c")) {
53               note.rp(7);
54           } else if (abcPitchClassName.equals("D")) {
55               note.rp(1);
56           } else if (abcPitchClassName.equals("D,")) {
57               note.lp(6);
58           } else if (abcPitchClassName.equals("d")) {
59               note.rp(8);
60           } else if (abcPitchClassName.equals("E")) {
61               note.rp(2);
62           } else if (abcPitchClassName.equals("E,")) {
63               note.lp(5);
64           } else if (abcPitchClassName.equals("e")) {
65               note.rp(9);
66           }
67           return note;
68       }
69       private Color getPitchClassColor(String letter) {
70           if ( letter.equals("C") ) {
71               return Color.BLUE;
72           } else if ( letter.equals("D") ) {
73               return Color.GREEN;
74           } else if ( letter.equals("E") ) {
75               return new Color(127,0,127);
76           } else {
77               return Color.BLACK;
78           }
79       }
80       public void play(String d) {
81           painter.setColor(color);
82           painter.paint(box);
83           painter.setColor(randomColor());
84           painter.draw(box);
85           if ( d.equals("1") ) {
86               note.play();
87           }
88       }
89       private static Color randomColor() {
90           int rv = (int)(Math.random()*256);
91           int gv = (int)(Math.random()*256);
92           int bv = (int)(Math.random()*256);
93           return new Color(rv,gv,bv);
94       }
95   }
96