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