Pitch.java
1    package chromesthesia1;
2    import note.SNote;
3    import painter.SPainter;
4    import shapes.SRectangle;
5    
6    import java.awt.*;
7    public class Pitch {
8        // INSTANCE VARIABLES
9        private String abcName;
10       private SPainter painter;
11       private SRectangle box;
12       private SNote note;
13       private Color color;
14   
15       public Pitch(String abcName, SPainter painter) {
16           this.abcName = abcName;
17           this.painter = painter;
18           this.box = new SRectangle(painter.painterHeight - 50, painter.painterWidth - 50);
19           this.note = createNoteForThisPitch(abcName);
20           this.color = getPitchClassColor(abcName.substring(0, 1).toUpperCase());
21       }
22   
23       public String toString() {
24           return "[ " + abcName + " | " + note.degree() + " | " + color + " ]";
25       }
26   
27       public String abcName() {
28           return abcName;
29       }
30   
31       private SNote createNoteForThisPitch(String abcPitchClassName) {
32           SNote note = new SNote();
33           if (abcPitchClassName.equals("C")) {
34               // nothing to do
35           } else if (abcPitchClassName.equals("C,")) {
36               note.lp(7);
37           } else if (abcPitchClassName.equals("c")) {
38               note.rp(7);
39           } else if (abcPitchClassName.equals("D")) {
40               note.rp(1);
41           } else if (abcPitchClassName.equals("D,")) {
42               note.lp(6);
43           } else if (abcPitchClassName.equals("d")) {
44               note.rp(8);
45           } else if (abcPitchClassName.equals("E")) {
46               note.rp(2);
47           } else if (abcPitchClassName.equals("E,")) {
48               note.lp(5);
49           } else if (abcPitchClassName.equals("e")) {
50               note.rp(9);
51           }
52           // making pitches Fs
53           else if (abcPitchClassName.equals("F")) {
54               note.rp(1);
55           } else if (abcPitchClassName.equals("F,")) {
56               note.lp(3);
57           } else if (abcPitchClassName.equals("f")) {
58               note.rp(5);
59               // Gs
60           } else if (abcPitchClassName.equals("G")) {
61               note.rp(2);
62           } else if (abcPitchClassName.equals("G,")) {
63               note.lp(4);
64           } else if (abcPitchClassName.equals("g")) {
65               note.rp(7);
66               // As
67           }else if (abcPitchClassName.equals("A")) {
68               note.rp(3);
69           } else if (abcPitchClassName.equals("A,")) {
70               note.lp(7);
71           } else if (abcPitchClassName.equals("a")) {
72               note.rp(9);
73           } // Bs
74           else if (abcPitchClassName.equals("B")) {
75               note.rp(1);
76           } else if (abcPitchClassName.equals("B,")) {
77               note.lp(2);
78           } else if (abcPitchClassName.equals("b")) {
79               note.rp(8);
80           }
81           return note;
82       }
83   
84       private Color getPitchClassColor(String letter) {
85           if (letter.equals("C")) {
86               return Color.BLUE;
87           } else if (letter.equals("D")) {
88               return Color.GREEN;
89           } else if (letter.equals("E")) {
90               return new Color(127, 0, 127);
91           } else if (letter.equals("F")) { // color for F
92               return new Color(240, 205, 200);
93           } else if (letter.equals("G")) { // color for G
94               return new Color(24, 205, 200);
95           } else if (letter.equals("A")) { // color for A
96               return new Color(240, 70, 200);
97           } else if (letter.equals("B")) { // color for B
98               return new Color(50, 75, 10);
99           } else {
100              return Color.BLACK;
101          }
102      }
103  
104      public void play(String d) {
105          painter.setColor(color);
106          painter.paint(box);
107          painter.setColor(randomColor());
108          painter.draw(box);
109          if (d.equals("1")) {
110              note.play();
111          }
112      }
113  
114      private static Color randomColor() {
115          int rv = (int) (Math.random() * 256);
116          int gv = (int) (Math.random() * 256);
117          int bv = (int) (Math.random() * 256);
118          return new Color(rv, gv, bv);
119      }
120  }
121  
122