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