1 /* 2 * The Pitch class models the pitch of a note in a manner that will facilitate 3 * the chromesthesic processing of the pitch. A Pitch object will have five 4 * properties: 5 * - String name | ABC notation pitch name 6 * - SPainter painter | the painting agent 7 * - Note note | a note that will be set to the pitch corresponding to the 8 * ABC notation pitch name 9 * - SRectangle box | an SRectangle object that will chromesthetically 10 * represent the pitch 11 * - Color color | the color associated with tthe pitch for the presumed 12 * chromesthete 13 */ 14 package chromesthesia0; 15 16 import note.SNote; 17 import painter.SPainter; 18 import shapes.SRectangle; 19 20 import java.awt.*; 21 22 public class Pitch { 23 24 // INSTANCE VARIABLES 25 private String abcName; 26 private SPainter painter; 27 private SRectangle box; 28 private SNote note; 29 private Color color; 30 31 public Pitch(String abcName, SPainter painter) { 32 this.abcName = abcName; 33 this.painter = painter; 34 this.box = new SRectangle(painter.painterHeight-50, painter.painterWidth-50); 35 this.note = createNoteForThisPitch(abcName); 36 this.color = getPitchClassColor(abcName.substring(0, 1).toUpperCase()); 37 } 38 39 public String toString() { 40 return "[ " + abcName + " | " + note.degree() + " | " + color + " ]"; 41 } 42 43 public String abcName() { 44 return abcName; 45 } 46 47 private SNote createNoteForThisPitch(String abcPitchClassName) { 48 SNote note = new SNote(); 49 if (abcPitchClassName.equals("C")) { 50 // nothing to do 51 } 52 else if (abcPitchClassName.equals("C,")) { 53 note.lp(7); 54 } 55 else if (abcPitchClassName.equals("c")) { 56 note.rp(7); 57 } 58 else if (abcPitchClassName.equals("D")) { 59 note.rp(1); 60 } 61 else if (abcPitchClassName.equals("D,")) { 62 note.lp(6); 63 } 64 else if (abcPitchClassName.equals("d")) { 65 note.rp(8); 66 } 67 else if (abcPitchClassName.equals("E")) { 68 note.rp(2); 69 } 70 else if (abcPitchClassName.equals("E,")) { 71 note.lp(5); 72 } 73 else if (abcPitchClassName.equals("e")) { 74 note.rp(9); 75 } 76 return note; 77 } 78 79 private Color getPitchClassColor(String letter) { 80 if (letter.equals("C")) { 81 return Color.BLUE; 82 } 83 else if (letter.equals("D")) { 84 return Color.GREEN; 85 } 86 else if (letter.equals("E")) { 87 return new Color(127, 0, 127); 88 } 89 else { 90 return Color.BLACK; 91 } 92 } 93 94 public void play(String d) { 95 painter.setColor(color); 96 painter.paint(box); 97 painter.setColor(randomColor()); 98 painter.draw(box); 99 if (d.equals("1")) { 100 note.play(); 101 } 102 } 103 104 private static Color randomColor() { 105 int rv = (int)(Math.random()*256); 106 int gv = (int)(Math.random()*256); 107 int bv = (int)(Math.random()*256); 108 return new Color(rv, gv, bv); 109 } 110 } 111