Pitch.java
1    /* 
2    *The pitch class models the pitch of a note in a manner that will facilitate 
3    * the chromestetic proccesing og 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 will be set to the pitch corresponding to the 
8    * ABC notation pitch nmae 
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   
15   package chromesthesia0;
16   
17   import note.SNote;
18   import painter.SPainter;
19   import shapes.SRectangle;
20   
21   import java.awt.*;
22   
23   public class Pitch {
24   
25       //INSTANCE VARIABLES
26       private String abcName;
27       private SPainter painter;
28       private SRectangle box;
29       private SNote note;
30       private Color color;
31   
32       public Pitch(String abcName, SPainter painter) {
33           this.abcName = abcName;
34           this.painter = painter;
35           this.box = new SRectangle(painter.painterHeight - 50, painter.painterWidth - 50);
36           this.note = createNoteForThisPitch(abcName);
37           this.color = getPitchClassColor(abcName.substring(0, 1).toUpperCase());
38       }
39   
40       public String toString() {
41           return "[ " + abcName + " | " + note.degree() + " | " + color + " ]";
42       }
43   
44       public String abcName() {
45           return abcName;
46       }
47   
48       private SNote createNoteForThisPitch(String abcPitchClassName) {
49           SNote note = new SNote();
50           if (abcPitchClassName.equals("C")) {
51               //nothing to do
52           } else if (abcPitchClassName.equals("C,")) {
53               note.lp(7);
54           } else if (abcPitchClassName.equals("c")) {
55               note.rp(7);
56           } else if ( abcPitchClassName.equals("D") ) {
57               note.rp(1);
58           } else if ( abcPitchClassName.equals("D,") ) {
59               note.lp(6);
60           } else if ( abcPitchClassName.equals("d") ) {
61               note.rp(8);
62           } else if ( abcPitchClassName.equals("E") ) {
63               note.rp(2);
64           } else if ( abcPitchClassName.equals("E,") ) {
65               note.lp(5);
66           } else if ( abcPitchClassName.equals("e") ) {
67               note.rp(9);
68           }
69           return note;
70       }
71   
72       private Color getPitchClassColor(String letter) {
73           if (letter.equals("C")) {
74               return Color.BLUE;
75           } else if (letter.equals("D")) {
76               return Color.GREEN;
77           } else if (letter.equals("E")) {
78               return new Color(127,0,127);
79           } else {
80               return Color.BLACK;
81           }
82       }
83   
84       public void play(String d) {
85           painter.setColor(color);
86           painter.paint(box);
87           painter.setColor(randomColor());
88           painter.draw(box);
89           if ( d.equals("1")) {
90               note.play();
91           }
92       }
93   
94       private static Color randomColor() {
95           int rv = (int)(Math.random()*256);
96           int gv = (int)(Math.random()*256);
97           int bv = (int)(Math.random()*256);
98           return new Color(rv,gv,bv);
99       }
100  
101  }
102