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   
15   package chromesthesia0;
16   
17   import java.awt.Color;
18   import note.SNote;
19   import painter.SPainter;
20   import shapes.SRectangle;
21   
22   public class Pitch {
23   
24       // INSTANCE VARIABLES
25       private String abcName;
26       private SPainter painter;
27       private SRectangle box;
28       private SNote note;
29       private Color color;
30   
31       public Pitch(String abcName, SPainter painter) {
32           this.abcName = abcName;
33           this.painter = painter;
34           this.box = new SRectangle(painter.painterHeight - 50, painter.painterWidth - 50);
35           this.note = createNoteForThisPitch(abcName);
36   
37           this.color = getPitchClassColor(abcName.substring(0, 1).toUpperCase());
38       }
39   
40       public String toString() {
41           return "[ " + abcName + " | " + note.degree() + " | " + color + " ]";
42   
43       }
44   
45       public String abcName() {
46           return abcName;
47   
48       }
49   
50       private SNote createNoteForThisPitch(String abcPitchClassName) {
51           SNote note = new SNote();
52           if (abcPitchClassName.equals("C")) {
53   
54               // nothing to do
55           } else if (abcPitchClassName.equals("C,")) {
56   
57               note.lp(7);
58           } else if (abcPitchClassName.equals("c")) {
59   
60               note.rp(7);
61           } else if (abcPitchClassName.equals("D")) {
62   
63               note.rp(1);
64           } else if (abcPitchClassName.equals("D,")) {
65   
66               note.lp(6);
67           } else if (abcPitchClassName.equals("d")) {
68   
69               note.rp(8);
70           } else if (abcPitchClassName.equals("E")) {
71   
72               note.rp(2);
73           } else if (abcPitchClassName.equals("E,")) {
74   
75               note.lp(5);
76           } else if (abcPitchClassName.equals("e")) {
77   
78               note.rp(9);
79           }
80   
81           return note;
82       }
83   
84       private Color getPitchClassColor(String letter) {
85           if (letter.equals("C")) {
86   
87               return Color.BLUE;
88           } else if (letter.equals("D")) {
89   
90               return Color.GREEN;
91           } else if (letter.equals("E")) {
92   
93               return new Color(127, 0, 127);
94           } else {
95   
96               return Color.BLACK;
97           }
98   
99       }
100  
101      public void play(String d) {
102          painter.setColor(color);
103          painter.paint(box);
104          painter.setColor(randomColor());
105          painter.draw(box);
106  
107          if (d.equals("1")) {
108              note.play();
109  
110          }
111  
112      }
113  
114      private static Color randomColor() {
115          int rv = (int) (Math.random() * 256);
116          int gv = (int) (Math.random() * 256);
117          int bv = (int) (Math.random() * 256);
118          return new Color(rv, gv, bv);
119  
120      }
121  }