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