Pitch.java
1    /* 
2     * The Pitch class models the pitch of a note in a manner that will facilitate the chromesthetic processing of the pitch. A Pitch object will have five properties: 
3     * -String name | ABC notation pitch name 
4     * -SPainter painter | the painting agent 
5     * -Note note | a note that will be set to the pitch corresponding to the ABC notation pitch name 
6     * -SRectangle box | an SRectangle object that will chromesthetically represent the pitch 
7     * -Color color | the color associated with the pitch for the presumed chromesthete 
8     */
9    
10   package chromesthesia0;
11   
12   import java.awt.Color;
13   import note.SNote;
14   import painter.SPainter;
15   import shapes.SRectangle;
16   
17   public class Pitch {
18       // INSTANCE VARIABLES
19       private String abcName;
20       private SPainter painter;
21       private SRectangle box;
22       private SNote note;
23       private Color color;
24   
25       public Pitch(String abcName, SPainter painter) {
26           this.abcName = abcName;
27           this.painter = painter;
28           this.box = new SRectangle(painter.painterHeight-50,painter.painterWidth-50);
29           this.note = createNoteForThisPitch(abcName);
30           this.color = getPitchClassColor(abcName.substring(0,1).toUpperCase());
31       }
32   
33       public String toString() {
34           return "[ " + abcName + " | " + note.degree() + " | " + color + " ]";
35       }
36   
37       public String abcName() {
38           return abcName;
39       }
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           }
62           return note;
63       }
64   
65       private Color getPitchClassColor(String letter) {
66           if ( letter.equals("C") ) {
67               return Color.BLUE;
68           } else if ( letter.equals("D") ) {
69               return Color.GREEN;
70           } else if ( letter.equals("E") ) {
71               return new Color(127, 0, 127);
72           } else {
73               return Color.BLACK;
74           }
75       }
76   
77       public void play(String d) {
78           painter.setColor(color);
79           painter.paint(box);
80           painter.setColor(randomColor());
81           painter.draw(box);
82           if ( d.equals("1") ) {
83               note.play();
84           }
85       }
86   
87       private static Color randomColor() {
88           int rv = (int)(Math.random()*256);
89           int gv = (int)(Math.random()*256);
90           int bv = (int)(Math.random()*256);
91           return new Color(rv,gv,bv);
92       }
93   }