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