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