Pitch.java
1    package Chromesthesia2;
2    
3    import note.SNote;
4    import painter.SPainter;
5    import shapes.SRectangle;
6    
7    import java.awt.*;
8    
9    public class Pitch {
10   
11       //INSTANCE VARIABLES
12       private String abcName;
13       private SPainter painter;
14       private  SRectangle box;
15       private SNote note;
16       private Color color;
17   
18       public Pitch(String abcName, SPainter painter) {
19           this.abcName = abcName;
20           this.painter = painter;
21           this.box = new SRectangle(painter.painterHeight-50, painter.painterWidth-50);
22           this.note = createNoteForThisPitch(abcName);
23           this.color = getPitchClassColor(abcName.substring(0, 1).toUpperCase());
24       }
25   
26       public String toString() {
27           return "[" + abcName + " | " + note.degree() + " | " + color + " ]";
28       }
29   
30       public String abcName() {
31           return abcName;
32       }
33   
34       private SNote createNoteForThisPitch(String abcPitchClassName) {
35           SNote note = new SNote();
36           if ( abcPitchClassName.equals("C")) {
37               //nothing to do
38           }
39           else if (abcPitchClassName.equals("C,")) {
40               note.lp(7);
41           }
42           else if (abcPitchClassName.equals("c")) {
43               note.rp(7);
44           }
45           else if (abcPitchClassName.equals("D")) {
46               note.rp(1);
47           }
48           else if (abcPitchClassName.equals("D,")) {
49               note.lp(6);
50           }
51           else if (abcPitchClassName.equals("d")) {
52               note.rp(8);
53           }
54           else if ( abcPitchClassName.equals("E")) {
55               note.rp(2);
56           }
57           else if (abcPitchClassName.equals("E,")) {
58               note.lp(5);
59           }
60           else if (abcPitchClassName.equals("e")) {
61               note.rp(9);
62           }
63           else if (abcPitchClassName.equals("F")) {
64               note.rp(3);
65           }
66           else if (abcPitchClassName.equals("F,")) {
67               note.lp(4);
68           }
69           else if (abcPitchClassName.equals("f")) {
70               note.rp(10);
71           }
72           else if (abcPitchClassName.equals("G")) {
73               note.rp(4);
74           }
75           else if (abcPitchClassName.equals("G,")) {
76               note.lp(3);
77           }
78           else if (abcPitchClassName.equals("g")) {
79               note.rp(11);
80           }
81           else if (abcPitchClassName.equals("A")) {
82               note.lp(2);
83           }
84           else if (abcPitchClassName.equals("A,")) {
85               note.lp(9);
86           }
87           else if  (abcPitchClassName.equals("a")) {
88               note.rp(5);
89           }
90           else if (abcPitchClassName.equals("B")) {
91               note.lp(1);
92           }
93           else if (abcPitchClassName.equals("B,")) {
94               note.lp(8);
95           }
96           else if (abcPitchClassName.equals("b")) {
97               note.rp(6);
98           }
99           return note;
100  
101      }
102  
103      private Color getPitchClassColor(String letter) {
104          if (letter.equals("A")) {
105              return new Color(0, 0, 255);
106          }
107          else if (letter.equals("B")) {
108              return new Color(0, 255, 0);
109          }
110          else if (letter.equals("C")) {
111              return new Color(127, 0, 127);
112          }
113          else if ( letter.equals("D")) {
114              return new Color(255, 255, 0);
115          }
116          else if (letter.equals("E")) {
117              return new Color(255, 0, 0);
118          }
119          else if (letter.equals("F")) {
120              return new Color(255, 127, 0);
121          }
122          else if (letter.equals("G")) {
123              return new Color(0, 255, 255);
124          }
125          else{
126              return Color.BLACK;
127          }
128      }
129  
130      public void play(String d) {
131          painter.setColor(color);
132          painter.paint(box);
133          painter.setColor(randomColor());
134          painter.draw(box);
135          if (d.equals("1")) {
136              note.play();
137          }
138          else if (d.equals("2")) {
139              note.x2(); note.play(); note.s2();
140          }
141          else if (d.equals("1/2")) {
142              note.s2(); note.play(); note.x2();
143          }
144          else if (d.equals("3")) {
145              note.x3(); note.play(); note.s3();
146          }
147          else if (d.equals("1/3")) {
148              note.s2(1/3); note.play(); note.x2(1/3);
149          }
150          else if (d.equals("2/3")) {
151              note.s2(2/3); note.play(); note.x2(2/3);
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  
165  
166  
167  
168  }
169