Pitch.java
1    
2     package chromesthesia0;
3    
4    import note.SNote;
5    import painter.SPainter;
6    import shapes.SRectangle;
7    
8    import java.awt.*;
9    
10   public class Pitch {
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( "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           }
54           return note;
55       }
56   
57       private Color getPitchClassColor(String letter) {
58           if (letter.equals( "C" )) {
59               return Color.BLUE;
60           } else if (letter.equals( "D" )) {
61               return Color.GREEN;
62           } else if (letter.equals( "E" )) {
63               return new Color( 127, 0, 127 );
64           } else {
65               return Color.BLACK;
66           }
67       }
68   
69       public void play(String d) {
70           painter.setColor( color );
71           painter.paint( box );
72           painter.setColor( randomColor() );
73           painter.draw( box );
74           if (d.equals( "1" )) {
75               note.play();
76           }
77       }
78   
79       private static Color randomColor() {
80           int rv = (int) (Math.random() * 256);
81           int gv = (int) (Math.random() * 256);
82           int bv = (int) (Math.random() * 256);
83           return new Color( rv, gv, bv );
84       }
85   }
86   
87