P.48 (Update Piano Class)


private String[] whiteKeys
  = { "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "\\" };
private String[] whiteNotes
  = { "3c", "3d", "3e", "3f", "3g", "3a", "3b", "4c", "4d", "4e", "4f", "4g" };
private Key[] pianoWhiteKey = new Key[12];
private String[] blackKeys
  = { "w", "e", "", "t", "y", "u", "", "o", "p", "", "]" };
private String[] blackNotes
  = { "3c#", "3d#", "", "3f#", "3g#", "3a#", "", "4c#", "4d#", "", "4f#" };
private Key[] pianoBlackKey = new Key[11];
public Piano() {
  super(800, 340, 1);
  for(int i=0; i<12; i++) {
    pianoWhiteKey[i] = new Key(whiteKeys[i], whiteNotes[i]+".wav",
                       "white-key.png", "white-key-down.png");     
    addObject(pianoWhiteKey[i], 22 + 32 + i*63, 140);
  }
  for(int i=0; i<12-1; i++) { // Add another loop to create black keys
    if(!blackKeys[i].equals("")) { // If black key name is not empty
      pianoBlackKey[i] = new Key(blackKeys[i], blackNotes[i]+".wav",
                         "black-key.png", "black-key-down.png");
      addObject(pianoBlackKey[i], 22 + (63/2) + 32 + i*63, 86);
    } // Shifted by half-width of white key
  }
}
Last modified on
Engineering DIY Workshop 2018