Pitch.java
1    package chromesthesia0;
2    import java.awt.Color;
3    import note.SNote;
4    import painter.SPainter;
5    import shapes.SRectangle;
6    public class Pitch {
7        private String abcName;
8        private SPainter painter;
9        private SRectangle box;
10       private SNote note;
11       private Color color;
12   
13       public Pitch(String abcName, SPainter painter) {
14           this.abcName = abcName;
15           this.painter = painter;
16           this.box = new SRectangle(painter.painterHeight-50,painter.painterWidth-50);
17           this.note = createNoteForThisPitch(abcName);
18           this.color = getPitchClassColor(abcName.substring(0,1).toUpperCase());
19       }
20   
21       public String toString() {
22           return "[ " + abcName + " | " + note.degree() + " | " + color + " ]";
23       }
24   
25       public String abcName() {
26           return abcName;
27       }
28   
29       private SNote createNoteForThisPitch(String abcPitchClassName) {
30           SNote note = new SNote();
31           if ( abcPitchClassName.equals("C") ) {
32               // nothing to do
33   
34           } else if ( abcPitchClassName.equals("C,") ) {
35               note.lp(7);
36           } else if ( abcPitchClassName.equals("c") ) {
37               note.rp(7);
38           } else if ( abcPitchClassName.equals("D") ) {
39               note.rp(1);
40           } else if ( abcPitchClassName.equals("D,") ) {
41               note.lp(6);
42           } else if ( abcPitchClassName.equals("d") ) {
43               note.rp(8);
44           } else if ( abcPitchClassName.equals("E") ) {
45               note.rp(2);
46           } else if ( abcPitchClassName.equals("E,") ) {
47               note.lp(5);
48           } else if ( abcPitchClassName.equals("e") ) {
49               note.rp(9);
50           }
51           return note;
52       }
53   
54       private Color getPitchClassColor(String letter) {
55           if ( letter.equals("C") ) {
56               return Color.BLUE;
57           } else if ( letter.equals("D") ) {
58               return Color.GREEN;
59           } else if ( letter.equals("E") ) {
60               return new Color(127,0,127);
61           } else {
62               return Color.BLACK;
63           }
64   
65       }
66   
67       public void play(String d) {
68           painter.setColor(color);
69           painter.paint(box);
70           painter.setColor(randomColor());
71           painter.draw(box);
72           if ( d.equals("1") ) {
73               note.play();
74           }
75       }
76   
77       private static Color randomColor() {
78           int rv = (int)(Math.random()*256);
79           int gv = (int)(Math.random()*256);
80           int bv = (int)(Math.random()*256);
81           return new Color(rv,gv,bv);
82       }
83   }