Pitch.java
1    package chromesthesia1;
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           } else if ( abcPitchClassName.equals("F") ) {
51               note.rp(10);
52           } else if ( abcPitchClassName.equals("F,") ) {
53               note.rp(8);
54           } else if ( abcPitchClassName.equals("f") ) {
55               note.rp(1);
56           }else if ( abcPitchClassName.equals("G") ) {
57               note.rp(3);
58           } else if ( abcPitchClassName.equals("G,") ) {
59               note.rp(5);
60           } else if ( abcPitchClassName.equals("g") ) {
61               note.rp(12);
62           } else if ( abcPitchClassName.equals("A") ) {
63               note.rp(7);
64           } else if ( abcPitchClassName.equals("A,") ) {
65               note.rp(6);
66           } else if ( abcPitchClassName.equals("a") ) {
67               note.rp(6);
68           } else if ( abcPitchClassName.equals("B") ) {
69               note.rp(2);
70           } else if ( abcPitchClassName.equals("B,") ) {
71               note.rp(1);
72           } else if ( abcPitchClassName.equals("b") ) {
73               note.rp(4);
74           }
75           return note;
76       }
77   
78       private Color getPitchClassColor(String letter) {
79           if ( letter.equals("C") ) {
80               return Color.BLUE;
81           } else if ( letter.equals("D") ) {
82               return Color.GREEN;
83           } else if ( letter.equals("E") ) {
84               return new Color(127,0,127);
85           } else if ( letter.equals("F") ) {
86               return Color.YELLOW;
87           } else if ( letter.equals("G") ) {
88               return Color.pink;
89           } else if ( letter.equals("A") ) {
90               return Color.ORANGE;
91           } else if ( letter.equals("B") ) {
92               return Color.RED;
93           } else {
94               return Color.BLACK;
95           }
96   
97       }
98   
99       public void play(String d) {
100          painter.setColor(color);
101          painter.paint(box);
102          painter.setColor(randomColor());
103          painter.draw(box);
104          if ( d.equals("1") ) {
105              note.play();
106          }
107      }
108  
109      private static Color randomColor() {
110          int rv = (int)(Math.random()*256);
111          int gv = (int)(Math.random()*256);
112          int bv = (int)(Math.random()*256);
113          return new Color(rv,gv,bv);
114      }
115  }