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