Pitch.java
1    /* 
2     * This program is little different then the first one we have been 
3     * add few more note f g a b 
4     * The Pitch class models the pitch of a note in a manner that will facilitate 
5     * the chromesthetic processing of the pitch. A Pitch object will have five 
6     * properties: 
7     * - String name | ABC notation pitch name 
8     * - SPainter painter | the painting agent 
9     * - Note note | a note that will be set to the pitch corresponding to the 
10    * ABC notation pitch name 
11    * - SRectangle box | an SRectangle object that will chromesthetically 
12    * represent the pitch 
13    * - Color color | the color associated with the pitch for the presumed 
14    * chromesthete 
15    */
16   package chromesthesia1;
17   import java.awt.Color;
18   import note.SNote;
19   import painter.SPainter;
20   import shapes.SRectangle;
21   public class Pitch {
22       // INSTANCE VARIABLES
23       private String abcName;
24       private SPainter painter;
25       private SRectangle box;
26       private SNote note;
27       private Color color;
28       public Pitch(String abcName, SPainter painter) {
29           this.abcName = abcName;
30           this.painter = painter;
31           this.box = new SRectangle(painter.painterHeight-50,painter.painterWidth-50);
32           this.note = createNoteForThisPitch(abcName);
33           this.color = getPitchClassColor(abcName.substring(0,1).toUpperCase());
34       }
35       public String toString() {
36           return "[ " + abcName + " | " + note.degree() + " | " + color + " ]";
37       }
38       public String abcName() {
39           return abcName;
40       }
41       private SNote createNoteForThisPitch(String abcPitchClassName) {
42           SNote note = new SNote();
43           if ( abcPitchClassName.equals("C") ) {
44   // nothing to do
45           } else if ( abcPitchClassName.equals("C,") ) {
46               note.lp(7);
47           } else if ( abcPitchClassName.equals("c") ) {
48               note.rp(7);
49           } else if ( abcPitchClassName.equals("D") ) {
50               note.rp(1);
51           } else if ( abcPitchClassName.equals("D,") ) {
52               note.lp(6);
53           } else if ( abcPitchClassName.equals("d") ) {
54               note.rp(8);
55           } else if ( abcPitchClassName.equals("E") ) {
56               note.rp(2);
57           } else if ( abcPitchClassName.equals("E,") ) {
58               note.lp(5);
59           } else if ( abcPitchClassName.equals("e") ) {
60               note.rp(9);
61           }else if ( abcPitchClassName.equals("F") ) {
62               note.rp(3);
63           } else if ( abcPitchClassName.equals("F,") ) {
64               note.lp(4);
65           } else if ( abcPitchClassName.equals("f") ) {
66               note.rp(10);
67           }else if ( abcPitchClassName.equals("G") ) {
68               note.rp(4);
69           } else if ( abcPitchClassName.equals("G,") ) {
70               note.lp(3);
71           } else if ( abcPitchClassName.equals("g") ) {
72               note.rp(11);
73           }else if ( abcPitchClassName.equals("A") ) {
74               note.rp(5);
75           } else if ( abcPitchClassName.equals("A,") ) {
76               note.lp(2);
77           } else if ( abcPitchClassName.equals("a") ) {
78               note.rp(12);
79           }else if ( abcPitchClassName.equals("B") ) {
80               note.rp(6);
81           } else if ( abcPitchClassName.equals("B,") ) {
82               note.lp(1);
83           } else if ( abcPitchClassName.equals("b") ) {
84               note.rp(13);
85           }
86           return note;
87       }
88       private Color getPitchClassColor(String letter) {
89           if ( letter.equals("C") ) {
90               return Color.BLACK;
91           } else if ( letter.equals("D") ) {
92               return Color.RED;
93           } else if ( letter.equals("E") ) {
94               return new Color(127, 0, 127);
95           }else if ( letter.equals("F") ) {
96               return Color.BLUE;
97           }else if ( letter.equals("G") ) {
98               return Color.YELLOW;
99           } else if ( letter.equals("A") ) {
100              return Color.MAGENTA;
101          } else if ( letter.equals("B") ) {
102              return Color.ORANGE;
103          } else {
104              return Color.GREEN;
105          }
106      }
107      public void play(String d) {
108          painter.setColor(color);
109          painter.paint(box);
110          painter.setColor(randomColor());
111          painter.draw(box);
112          if ( d.equals("1") ) {
113              note.play();
114          }
115      }
116      private static Color randomColor() {
117          int rv = (int)(Math.random()*256);
118          int gv = (int)(Math.random()*256);
119          int bv = (int)(Math.random()*256);
120          return new Color(rv,gv,bv);
121      }
122  }