1 package chromesthesia0; 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 return note; 67 } 68 private Color getPitchClassColor(String letter) 69 { 70 if ( letter.equals("C") ) 71 { 72 return Color.BLUE; 73 } 74 else if ( letter.equals("D") ) 75 { 76 return Color.GREEN; 77 } 78 else if ( letter.equals("E") ) 79 { 80 return new Color(127,0,127); 81 } 82 else 83 { 84 return Color.BLACK; 85 } 86 } 87 88 public void play(String d) 89 { 90 painter.setColor(color); 91 painter.paint(box); 92 painter.setColor(randomColor()); 93 painter.draw(box); 94 if ( d.equals("1") ) 95 { 96 note.play(); 97 } 98 } 99 private static Color randomColor() 100 { 101 int rv = (int)(Math.random()*256); 102 int gv = (int)(Math.random()*256); 103 int bv = (int)(Math.random()*256); 104 return new Color(rv,gv,bv); 105 } 106 } 107