Pitch.java
1    /*The Pitch class models the pitch of a note in a manner that will facilitate 
2    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    package chromesthesia1;
10   
11   import java.awt.Color;
12   import note.SNote;
13   import painter.SPainter;
14   import shapes.SRectangle;
15   
16   public class Pitch {
17       //Instance Variables
18       private String abcName;
19       private SPainter painter;
20       private SRectangle box;
21       private SNote note;
22       private Color color;
23   
24       public Pitch(String abcName, SPainter painter) {
25           this.abcName = abcName;
26           this.painter = painter;
27           this.box = new SRectangle(painter.painterHeight - 50, painter.painterWidth - 50);
28           this.note = createNoteForThisPitch(abcName);
29           this.color = getPitchClassColor(abcName.substring(0, 1).toUpperCase());
30       }
31   
32       public String toString() {
33           return "[" + abcName + "|" + note.degree() + "|" + color + "]";
34       }
35   
36       public String abcName() {
37           return abcName;
38       }
39   
40       private SNote createNoteForThisPitch(String abcPitchClassName) {
41           SNote note = new SNote();
42           if (abcPitchClassName.equals("C")) {
43               //nothing to do
44           } else if (abcPitchClassName.equals("C,")) {
45               note.lp(7);
46           } else if (abcPitchClassName.equals("c")) {
47               note.rp(7);
48           } else if (abcPitchClassName.equals("D")) {
49               note.rp(1);
50           } else if (abcPitchClassName.equals("D,")) {
51               note.lp(6);
52           } else if (abcPitchClassName.equals("d")) {
53               note.rp(8);
54           } else if (abcPitchClassName.equals("E")) {
55               note.rp(2);
56           } else if (abcPitchClassName.equals("E,")) {
57               note.lp(5);
58           } else if (abcPitchClassName.equals("e")) {
59               note.rp(9);
60           } else if (abcPitchClassName.equals("F")) {
61               note.rp(3);
62           } else if (abcPitchClassName.equals("F,")) {
63               note.lp(4);
64           } else if (abcPitchClassName.equals("f")) {
65               note.rp(10);
66           } else if (abcPitchClassName.equals("G")) {
67               note.rp(4);
68           } else if (abcPitchClassName.equals("G,")) {
69               note.lp(3);
70           } else if (abcPitchClassName.equals("g")) {
71               note.rp(11);
72           } else if ( abcPitchClassName.equals("A") ) {
73           note.rp(5);
74           } else if ( abcPitchClassName.equals("A,") ) {
75           note.lp(2);
76           } else if ( abcPitchClassName.equals("a") ) {
77           note.rp(12);
78           } else if ( abcPitchClassName.equals("B") ) {
79           note.rp(6);
80           } else if ( abcPitchClassName.equals("B,") ) {
81           note.lp(1);
82           } else if ( abcPitchClassName.equals("b") ) {
83           note.rp(13);
84           }
85           return note;
86       }
87   
88       private Color getPitchClassColor(String letter) {
89           if (letter.equals("C")) {
90               return Color.BLUE;
91           } else if (letter.equals("D")) {
92               return Color.GREEN;
93           } else if (letter.equals("E")) {
94               return new Color(127, 0, 127);
95           } else if (letter.equals("F")) {
96               return Color.RED;
97           } else if (letter.equals("G")) {
98               return Color.PINK;
99           } else if (letter.equals("A")) {
100              return Color.ORANGE;
101          } else if (letter.equals("B")) {
102              return Color.MAGENTA;
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  }