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 chromesthete 
12    */
13   package chromesthesia0;
14   
15   import java.awt.Color;
16   import note.SNote;
17   import painter.SPainter;
18   import shapes.SRectangle;
19   
20   public class Pitch {
21   
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       public Pitch(String abcName, SPainter painter) {
30           this.abcName = abcName;
31           this.painter = painter;
32           this.box = new SRectangle(painter.painterHeight-50, painter.painterWidth-50);
33           this.note = createNoteForThisPitch(abcName);
34           this.color = getPitchClassColor(abcName.substring(0,1).toUpperCase());
35       }
36   
37       public String toString() {
38           return "[ " + abcName + " | " + note.degree() + " | " + color + " ]";
39       }
40   
41       public String abcName() {
42           return abcName;
43       }
44   
45       private SNote createNoteForThisPitch(String abcPitchClassName) {
46           SNote note = new SNote();
47           if (abcPitchClassName.equals("C")) {
48               // nothing to do
49           } else if (abcPitchClassName.equals("C,")) {
50               note.lp(7);
51           } else if (abcPitchClassName.equals("c")) {
52               note.rp(7);
53           } else if (abcPitchClassName.equals("D")) {
54               note.rp(1);
55           } else if (abcPitchClassName.equals("D,")) {
56               note.lp(6);
57           } else if (abcPitchClassName.equals("d")) {
58               note.rp(8);
59           } else if (abcPitchClassName.equals("E")) {
60               note.rp(2);
61           } else if (abcPitchClassName.equals("E,")) {
62               note.lp(5);
63           } else if (abcPitchClassName.equals("e")) {
64               note.rp(9);
65           }
66           return note;
67       }
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   
81       public void play(String d) {
82           painter.setColor(color);
83           painter.paint(box);
84           painter.setColor(randomColor());
85           painter.draw(box);
86           if (d.equals("1")) {
87               note.play();
88           }
89       }
90   
91       private static Color randomColor() {
92           int rv = (int)(Math.random()*256);
93           int gv = (int)(Math.random()*256);
94           int bv = (int)(Math.random()*256);
95           return new Color(rv,gv,bv);
96       }
97   }
98