Pitch.java
1    /* 
2    ThPitch 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           else if(abcPitchClassName.equals("C,")){
52               note.lp(7);
53           } else if (abcPitchClassName.equals("c")){
54               note.rp(7);
55           } else if (abcPitchClassName.equals("D")){
56               note.rp(1);
57           } else if (abcPitchClassName.equals("D,")){
58               note.lp(6);
59           } else if (abcPitchClassName.equals("d")) {
60               note.rp(8);
61           } else if(abcPitchClassName.equals("E")){
62               note.rp(2);
63           } else if(abcPitchClassName.equals("E,")){
64               note.lp(5);
65           } else if (abcPitchClassName.equals("e")){
66               note.rp(9);
67           } else if (abcPitchClassName.equals("A")){
68               note.lp(2);
69           } else if (abcPitchClassName.equals("A,")){
70               note.lp(9);
71           } else if (abcPitchClassName.equals("a")){
72               note.rp(5);
73           } else if (abcPitchClassName.equals("B")){
74               note.lp(1);
75           } else if (abcPitchClassName.equals("B,")){
76               note.lp(8);
77           } else if (abcPitchClassName.equals("b")){
78               note.rp(6);
79           } else if (abcPitchClassName.equals("F")){
80               note.rp(3);
81           } else if (abcPitchClassName.equals("F,")){
82               note.lp(4);
83           } else if (abcPitchClassName.equals("f")){
84               note.rp(10);
85           } else if (abcPitchClassName.equals("G")){
86               note.rp(4);
87           } else if (abcPitchClassName.equals("G,")){
88               note.lp(3);
89           } else if (abcPitchClassName.equals("g")){
90               note.rp(11);
91           }
92           return note;
93       }
94   
95       private Color getPitchClassColor(String letter) {
96           if (letter.equals("C")) {
97               return new Color(127,0,127);
98           } else if (letter.equals("D")) {
99               return new Color(255,255,0);
100          } else if (letter.equals("E")) {
101              return new Color(255,0,0);
102          } else if (letter.equals("A")) {
103              return new Color(0,0,255);
104          } else if (letter.equals("B")) {
105              return new Color(0,255,0);
106          } else if (letter.equals("F")) {
107              return new Color(255,127,0);
108          } else if (letter.equals("G")) {
109              return new Color(0,255,255);
110          } else {
111              return Color.BLACK;
112          }
113      }
114  
115      public void play(String d){
116          painter.setColor(color);
117          painter.paint(box);
118          painter.setColor(randomColor());
119          painter.draw(box);
120          if(d.equals("1")){
121              note.play();
122          } else if (d.equals("2")){
123              note.x2();
124              note.play();
125              note.s2();
126          } else if (d.equals("1/2")){
127              note.s2();
128              note.play();
129              note.x2();
130          } else if (d.equals("3")){
131              note.x3();
132              note.play();
133              note.s3();
134          } else if (d.equals("1/3")){
135              note.s3();
136              note.play();
137              note.x3();
138          } else if (d.equals("2/3")) {
139              note.s3();
140              note.x2();
141              note.play();
142              note.s2();
143              note.x3();
144          }
145      }
146  
147      private static Color randomColor() {
148          int rv=(int)(Math.random()*256);
149          int gv=(int)(Math.random()*256);
150          int bv=(int)(Math.random()*256);
151          return new Color(rv,gv,bv);
152      }
153  }
154