Pitch.java
1    /* 
2     * The Pitch class models the pitch of a note in a manner that will facilitate 
3     * the chromesthesic 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 tthe pitch for the presumed 
12    *  chromesthete 
13    */
14   package chromesthesia2;
15   
16   import note.SNote;
17   import painter.SPainter;
18   import shapes.SRectangle;
19   
20   import java.awt.*;
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           this.color = getPitchClassColor(abcName.substring(0, 1).toUpperCase());
37       }
38   
39       public String toString() {
40           return "[ " + abcName + " | " + note.degree() + " | " + color + " ]";
41       }
42   
43       public String abcName() {
44           return abcName;
45       }
46   
47       private SNote createNoteForThisPitch(String abcPitchClassName) {
48           SNote note = new SNote();
49           if (abcPitchClassName.equals("C")) {
50               // nothing to do
51           }
52           else if (abcPitchClassName.equals("C,")) {
53               note.lp(7);
54           }
55           else if (abcPitchClassName.equals("c")) {
56               note.rp(7);
57           }
58           else if (abcPitchClassName.equals("D")) {
59               note.rp(1);
60           }
61           else if (abcPitchClassName.equals("D,")) {
62               note.lp(6);
63           }
64           else if (abcPitchClassName.equals("d")) {
65               note.rp(8);
66           }
67           else if (abcPitchClassName.equals("E")) {
68               note.rp(2);
69           }
70           else if (abcPitchClassName.equals("E,")) {
71               note.lp(5);
72           }
73           else if (abcPitchClassName.equals("e")) {
74               note.rp(9);
75           }
76           else if (abcPitchClassName.equals("F")) {
77               note.rp(6);
78           }
79           else if (abcPitchClassName.equals("F,")) {
80               note.lp(8);
81           }
82           else if (abcPitchClassName.equals("f")) {
83               note.rp(3);
84           }
85           return note;
86       }
87   
88       private Color getPitchClassColor(String letter) {
89           if (letter.equals("C")) {
90               return new Color(127, 0, 127);
91           }
92           else if (letter.equals("D")) {
93               return new Color(255, 255, 0);
94           }
95           else if (letter.equals("E")) {
96               return new Color(255, 0, 0);
97           }
98           else if (letter.equals("F")) {
99               return new Color(255, 127, 0);
100          }
101          else if (letter.equals("F,")) {
102              return new Color(55, 161, 207);
103          }
104          else if (letter.equals("f")) {
105              return new Color(43, 92, 207);
106          }
107          else if (letter.equals("G")) {
108              return new Color(0, 255, 255);
109          }
110          else if (letter.equals("G,")) {
111              return new Color(207, 37, 167);
112          }
113          else if (letter.equals("g")) {
114              return new Color(207, 43, 80);
115          }
116          else if (letter.equals("A")) {
117              return new Color(0, 0, 255);
118          }
119          else if (letter.equals("A,")) {
120              return new Color(207, 200, 56);
121          }
122          else if (letter.equals("a")) {
123              return new Color(125, 207, 49);
124          }
125          else if (letter.equals("B")) {
126              return new Color(0, 255, 0);
127          }
128          else if (letter.equals("B,")) {
129              return new Color(55, 193, 116);
130          }
131          else if (letter.equals("b")) {
132              return new Color(140, 167, 126, 176);
133          }
134          else {
135              return Color.BLACK;
136          }
137      }
138  
139      public void play(String d) {
140          painter.setColor(color);
141          painter.paint(box);
142          painter.setColor(randomColor());
143          painter.draw(box);
144          if (d.equals("1")) {
145              note.play();
146          }
147          else if (d.equals("2")) {
148              note.x2();
149              note.play();
150              note.s2();
151          }
152          else if (d.equals("1/2")) {
153              note.s2();
154              note.play();
155              note.x2();
156          }
157          else if (d.equals("3")) {
158              note.x3();
159              note.play();
160              note.s3();
161          }
162          else if (d.equals("1/3")) {
163              note.s3();
164              note.play();
165              note.x3();
166          }
167          else if (d.equals("2/3")) {
168              note.x2();
169              note.s3();
170              note.play();
171              note.x3();
172              note.s2();
173          }
174      }
175  
176      private static Color randomColor() {
177          int rv = (int)(Math.random()*256);
178          int gv = (int)(Math.random()*256);
179          int bv = (int)(Math.random()*256);
180          return new Color(rv, gv, bv);
181      }
182  }
183  
184