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 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   
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     // 0
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);     // 0
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);    //0
64           } else if ( abcPitchClassName.equals("E,") ) {
65               note.lp(5);     //-
66           } else if ( abcPitchClassName.equals("e") ) {
67               note.rp(9);
68           } else if ( abcPitchClassName.equals("F") ) {
69               note.rp(3);     //0
70           } else if ( abcPitchClassName.equals("F,") ) {
71               note.lp(4);     //-
72           } else if ( abcPitchClassName.equals("f") ) {
73               note.rp(10);
74           } else if ( abcPitchClassName.equals("G") ) {
75               note.rp(4);     //0
76           } else if ( abcPitchClassName.equals("G,") ) {
77               note.lp(3);     //-
78           } else if ( abcPitchClassName.equals("g") ) {
79               note.rp(11);
80           } else if ( abcPitchClassName.equals("A") ) {
81               note.rp(5);     //0
82           } else if ( abcPitchClassName.equals("A,") ) {
83               note.lp(2);     //-
84           } else if ( abcPitchClassName.equals("a") ) {
85               note.rp(12);
86           } else if ( abcPitchClassName.equals("B") ) {
87               note.rp(6);     //0
88           } else if ( abcPitchClassName.equals("B,") ) {
89               note.lp(1);     //-
90           } else if ( abcPitchClassName.equals("b") ) {
91               note.rp(13);
92           }
93           return note;
94       }
95   
96       private Color getPitchClassColor(String letter) {
97           if (letter.equals("C")) {
98               return Color.BLUE;
99           } else if (letter.equals("D")) {
100              return Color.GREEN;
101          } else if (letter.equals("E")) {
102              return new Color(127,0,127);
103          } else {
104              return Color.BLACK;
105          }
106      }
107  
108      public void play(String d) {
109          painter.setColor(color);
110          painter.paint(box);
111          painter.setColor(randomColor());
112          painter.draw(box);
113          if ( d.equals("1")) {
114              note.play();
115          }
116      }
117  
118      private static Color randomColor() {
119          int rv = (int)(Math.random()*256);
120          int gv = (int)(Math.random()*256);
121          int bv = (int)(Math.random()*256);
122          return new Color(rv,gv,bv);
123      }
124  
125  }
126