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