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