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