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