Pitch.java
1    
2    package chromesthesia2;
3    
4    import java.awt.Color;
5    
6    import note.SNote;
7    import painter.SPainter;
8    import shapes.SRectangle;
9    
10   public class Pitch {
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           } else if (abcPitchClassName.equals("C,")) {
39               note.lp(7);
40           } else if (abcPitchClassName.equals("c")) {
41               note.rp(7);
42           } else if (abcPitchClassName.equals("D")) {
43               note.rp(1);
44           } else if (abcPitchClassName.equals("D,")) {
45               note.lp(6);
46           } else if (abcPitchClassName.equals("d")) {
47               note.rp(8);
48           } else if (abcPitchClassName.equals("E")) {
49               note.rp(2);
50           } else if (abcPitchClassName.equals("E,")) {
51               note.lp(5);
52           } else if (abcPitchClassName.equals("e")) {
53               note.rp(9);
54           } else if (abcPitchClassName.equals("F")) {
55               note.rp(3);
56           } else if (abcPitchClassName.equals("F, ")) {
57               note.lp(6);
58           } else if (abcPitchClassName.equals("f")) {
59               note.rp(10);
60           } else if (abcPitchClassName.equals("G")) {
61               note.rp(4);
62           } else if (abcPitchClassName.equals("G, ")) {
63               note.lp(3);
64           } else if (abcPitchClassName.equals("g")) {
65               note.rp(11);
66           } else if (abcPitchClassName.equals("A")) {
67               note.rp(5);
68           } else if (abcPitchClassName.equals("A, ")) {
69               note.lp(2);
70           } else if (abcPitchClassName.equals("a")) {
71               note.rp(12);
72           } else if (abcPitchClassName.equals("B")) {
73               note.rp(5);
74           } else if (abcPitchClassName.equals("B, ")) {
75               note.lp(1);
76           } else if (abcPitchClassName.equals("b")) {
77               note.rp(13);
78           }
79           return note;
80       }
81   
82   
83       private Color getPitchClassColor(String letter){
84           if (letter.equals("C")) {
85               return new Color(127,0,127);
86           } else if (letter.equals("D")) {
87               return new Color(255,255,0);
88           } else if (letter.equals("E")) {
89               return new Color(255, 0, 0);
90           } else if ( letter.equals("F") ) {
91               return new Color(255,127,0);
92           } else if (letter.equals( "G" )) {
93               return new Color(0,255,255);
94           } else if (letter.equals( "A" )) {
95               return new Color(0,0,255);
96           } else if (letter.equals( "B" )) {
97               return new Color(0,255,0);
98           } else {
99               return Color.BLACK;
100          }
101      }
102  
103  
104  
105  
106  
107      public void play(String d) {
108          painter.setColor(color);
109          painter.paint(box);
110          painter.setColor(randomColor());
111          painter.draw(box);
112  
113          if (d.equals("1")) {
114              note.play();
115          }
116          else if (d.equals("2")) {
117              note.x2();
118              note.play();
119              note.s2();
120          }
121          else if (d.equals("1/2")) {
122              note.s2();
123              note.play();
124              note.x2();
125          }
126          else if (d.equals("3")) {
127              note.x3();
128              note.play();
129              note.s3();
130          }
131          else if (d.equals("1/3")) {
132              note.s3();
133              note.play();
134              note.x3();
135          }
136          else if (d.equals("2/3")) {
137              note.s3();
138              note.x2();
139              note.play();
140              note.s2();
141              note.x3();
142          }
143  
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