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   
15   package chromesthesia2;
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           } else if (abcPitchClassName.equals("F")) {
69               note.rp(3);
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("B")) {
75               note.rp(4);
76           } else if (abcPitchClassName.equals("B,")) {
77               note.lp(3);
78           } else if (abcPitchClassName.equals("b")) {
79               note.rp(11);
80           } else if (abcPitchClassName.equals("G")) {
81               note.rp(5);
82           } else if (abcPitchClassName.equals("G,")) {
83               note.lp(2);
84           } else if ( abcPitchClassName.equals("g")) {
85               note.rp(12);
86           } else if  (abcPitchClassName.equals("A")) {
87               note.rp(6);
88           } else if  (abcPitchClassName.equals("A,")) {
89               note.lp(1);
90           } else if  (abcPitchClassName.equals("a")) {
91               note.rp(13);
92           }
93           return note;
94   
95       }
96   
97       private Color getPitchClassColor(String letter) {
98           if (letter.equals("C")) {
99               return new Color(127, 0, 127);
100          } else if (letter.equals("D")) {
101              return new Color(255, 255, 0);
102          } else if (letter.equals("E")) {
103              return new Color(255, 0, 0);
104          } else if (letter.equals("F"))  {
105              return new Color(255, 127, 0);
106          } else if (letter.equals("G")) {
107              return new Color(0, 255, 255);
108          } else if (letter.equals("A")) {
109              return new Color(0, 0, 255);
110          } else if (letter.equals("B")) {
111              return new Color(0, 255, 0);
112          } else {
113              return Color.BLACK;
114          }
115      }
116  
117      public void play (String d) {
118          painter.setColor(color);
119          painter.paint(box);
120          painter.setColor(randomColor());
121          painter.draw(box);
122          if ( d.equals("1")) {
123              note.play();
124          } else if (d.equals("2")) {
125              note.x2(); note.play(); note.s2();
126          } else if (d.equals("1/2")) {
127              note.s2(); note.play(); note.x2();
128          } else if (d.equals("3")) {
129              note.x3(); note.play(); note.s3();
130          } else if (d.equals("1/3")) {
131              note.s3(); note.play(); note.x3();
132          } else if (d.equals("2/3")) {
133              note.s3(2); note.play(); note.x3(2);
134          }
135      }
136  
137      private static Color randomColor() {
138          int rv = (int) (Math.random()*256);
139          int gv = (int) (Math.random()*256);
140          int bv = (int) (Math.random()*256);
141          return new Color(rv, gv, bv);
142      }
143  }
144