Pitch.java
1    /* 
2     * The Pitch class models the pitch of a note in a manner that will faciliate 
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 represent the pitch 
10    * - Color color associated with the pitch for the presumed 
11    *   chromesthete 
12    */
13   package chromesthesia2;
14   
15   import java.awt.Color;
16   import note.SNote;
17   import painter.SPainter;
18   import shapes.SRectangle;
19   
20   public class Pitch {
21       // INSTANCE VARIABLES
22       private String abcName;
23       private SPainter painter;
24       private SRectangle box;
25       private SNote note;
26       private Color color;
27   
28       public Pitch(String abcName, SPainter painter) {
29           this.abcName = abcName;
30           this.painter = painter;
31           this.box = new SRectangle(painter.painterHeight-50,painter.painterWidth-50);
32           this.note = createNoteForThisPitch(abcName);
33           this.color = getPitchClassColor(abcName.substring(0,1).toUpperCase());
34       }
35       public String toString() {
36           return "[ " + abcName + " | " + note.degree() + " | " + color + " ]";
37       }
38       public String abcName() {
39           return abcName;
40       }
41       private SNote createNoteForThisPitch(String abcPitchClassName) {
42           SNote note = new SNote();
43           if (abcPitchClassName.equals("C")) {
44               // nothing to do
45           } else if (abcPitchClassName.equals("C,")) {
46               note.lp(7);
47           } else if (abcPitchClassName.equals("c")) {
48               note.rp(7);
49           } else if ( abcPitchClassName.equals("D") ) {
50               note.rp(1);
51           }  else if ( abcPitchClassName.equals("D,") ) {
52               note.lp(6);
53           } else if ( abcPitchClassName.equals("d") ) {
54               note.rp(8);
55           } else if ( abcPitchClassName.equals("E") ) {
56               note.rp(2);
57           } else if ( abcPitchClassName.equals("E,") ) {
58               note.lp(5);
59           } else if ( abcPitchClassName.equals("e") ) {
60               note.rp(9);
61           } else if ( abcPitchClassName.equals("F") ) {
62               note.rp(1);
63           } else if ( abcPitchClassName.equals("F,") ) {
64               note.lp(6);
65           } else if ( abcPitchClassName.equals("f") ) {
66               note.rp(8);
67           } else if ( abcPitchClassName.equals("G") ) {
68               note.rp(1);
69           } else if ( abcPitchClassName.equals("G,") ) {
70               note.lp(6);
71           } else if ( abcPitchClassName.equals("g") ) {
72               note.rp(8);
73           } else if ( abcPitchClassName.equals("A") ) {
74               note.rp(1);
75           } else if ( abcPitchClassName.equals("A,") ) {
76               note.lp(6);
77           } else if ( abcPitchClassName.equals("a") ) {
78               note.rp(8);
79           } else if ( abcPitchClassName.equals("B") ) {
80               note.rp(1);
81           } else if ( abcPitchClassName.equals("B,") ) {
82               note.lp(6);
83           } else if ( abcPitchClassName.equals("b") ) {
84               note.rp(8);
85           }
86           return note;
87       }
88       private Color getPitchClassColor(String letter) {
89           if ( letter.equals("A") ) {
90               return new Color(0, 0, 225);
91           } else if ( letter.equals("B") ) {
92               return new Color(0, 225, 0);
93           } else if ( letter.equals("C") ) {
94               return new Color(127,0,127);
95           } else if ( letter.equals("D") ) {
96               return new Color(255, 225, 0);
97           }  else if ( letter.equals("E") ) {
98               return new Color(225, 0, 0);
99           } else if ( letter.equals("F") ) {
100              return new Color(225,127,0);
101          } else if ( letter.equals("G") ) {
102              return new Color(0,225,225);
103          } else {
104              return Color.BLACK;
105          }
106      }
107      public void play(String d) {
108          painter.setColor(color);
109          painter.paint(box);
110          painter.setColor(randomColor());
111          painter.draw(box);
112          if ( d.equals("1") ) {
113              note.play();
114          } else if ( d.equals("2") ) {
115              note.x2();
116              note.play();
117              note.s2();
118          } else if ( d.equals("1/2") ) {
119              note.s2();
120              note.play();
121              note.x2();
122          } else if ( d.equals("3") ) {
123              note.x3();
124              note.play();
125              note.s3();
126          } else if ( d.equals("1/3") ) {
127              note.s3();
128              note.play();
129              note.x3();
130          } else if ( d.equals("2/3") ) {
131              note.s3();
132              note.s3();
133              note.play();
134              note.x3();
135              note.x3();
136          }
137      }
138      private static Color randomColor() {
139          int rv = (int)(Math.random()*256);
140          int gv = (int)(Math.random()*256);
141          int bv = (int)(Math.random()*256);
142          return new Color(rv,gv,bv);
143      }
144  }
145