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