Pitch.java
1    
2    
3    package chromesthesia1;
4    
5    import note.SNote;
6    import painter.SPainter;
7    import shapes.SRectangle;
8    
9    import java.awt.*;
10   
11   public class Pitch {
12       private String abcName;
13       private SPainter painter;
14       private SRectangle box;
15       private SNote note;
16       private Color color;
17   
18       public Pitch(String abcName, SPainter painter) {
19           this.abcName = abcName;
20           this.painter = painter;
21           this.box = new SRectangle( painter.painterHeight - 50, painter.painterWidth - 50 );
22           this.note = createNoteForThisPitch( abcName );
23           this.color = getPitchClassColor( abcName.substring( 0, 1 ).toUpperCase() );
24       }
25   
26       public String toString() {
27           return "[ " + abcName + " | " + note.degree() + " | " + color + " ]";
28       }
29   
30       public String abcName() {
31           return abcName;
32       }
33   
34       private SNote createNoteForThisPitch(String abcPitchClassName) {
35           SNote note = new SNote();
36           if (abcPitchClassName.equals( "C" )) {
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 if (letter.equals( "F" )) {
65               return Color.RED;
66           } else if (letter.equals( "G" )) {
67               return Color.CYAN;
68           } else if (letter.equals( "A" )) {
69               return Color.YELLOW;
70           } else if (letter.equals( "B" )) {
71               return Color.ORANGE;
72           }
73           else {
74               return Color.BLACK;
75           }
76       }
77   
78       public void play(String d) {
79           painter.setColor( color );
80           painter.paint( box );
81           painter.setColor( randomColor() );
82           painter.draw( box );
83           if (d.equals( "1" )) {
84               note.play();
85           }
86       }
87   
88       private static Color randomColor() {
89           int rv = (int) (Math.random() * 256);
90           int gv = (int) (Math.random() * 256);
91           int bv = (int) (Math.random() * 256);
92           return new Color( rv, gv, bv );
93       }
94   }
95   
96