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