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