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