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