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