12. Expansion: place a new key on the keyboard that is activated by pressing ‘m’. Have the ‘m’ key play a random note, one of the notes used on the 12 white keys already on the keyboard.
13. Expansion: place a new key on the keyboard that is activated by pressing ‘n’. Have the ‘n’ key play a random note, one of the notes used on the
14 black keys already on the keyboard.
15. Expansion: place a new key on the keyboard that is activated by pressing ‘b’. Create a chord, a combination of two or more notes played at the same time. Have a new key play a chord of three notes of your choosing when pressing the ‘b’ key.
16. Expansion: program each white key so that after every 10 times that the white key is pressed, a random note is played from among the 12 white keys. Write this segment using a constant:
o final int KEYPRESS_LIMIT = 10;
o Use KEYPRESS_LIMIT everywhere you might be tempted to use the number 10.
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
/**
* A piano that can be played with the computer keyboard.
*
* @author Michael Kölling
* @version 1.0
*/
public class Piano extends World
{
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 String[] blackKeys =
{ "W", "E", "", "T", "Y", "U", "", "O", "P", "", "]" };
private String[] blackNotes =
{ "3c#", "3d#", "", "3f#", "3g#", "3a#", "", "4c#", "4d#", "", "4f#" };
/**
* Make the piano. This means mostly, apart from defining the size,
* making the keys and placing them into the world.
*/
public Piano()
{
super(800, 340, 1);
makeKeys();
showText("Click 'Run', then use your keyboard to play", 400, 320);
}
/**
* Create the piano keys (white and black) and place them in the world.
*/
private void makeKeys()
{
// make the white keys
for(int i = 0; i < whiteKeys.length; i++) {
Key key = new Key(whiteKeys[i], whiteNotes[i]+".wav", "white-key.png", "white-key-down.png");
addObject(key, i*63 + 54, 140);
}
// make the black keys
for(int i = 0; i < blackKeys.length; i++) {
if( ! blackKeys[i].equals("") ) {
Key key = new Key(blackKeys[i], blackNotes[i]+".wav", "black-key.png", "black-key-down.png");
addObject(key, i*63 + 85, 86);
}
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
/**
* A piano that can be played with the computer keyboard.
*
* @author Michael Kölling
* @version 1.0
*/
public class Piano extends World
{
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 String[] blackKeys =
{ "W", "E", "", "T", "Y", "U", "", "O", "P", "", "]" };
private String[] blackNotes =
{ "3c#", "3d#", "", "3f#", "3g#", "3a#", "", "4c#", "4d#", "", "4f#" };
/**
* Make the piano. This means mostly, apart from defining the size,
* making the keys and placing them into the world.
*/
public Piano()
{
super(800, 340, 1);
makeKeys();
showText("Click 'Run', then use your keyboard to play", 400, 320);
}
/**
* Create the piano keys (white and black) and place them in the world.
*/
private void makeKeys()
{
// make the white keys
for(int i = 0; i < whiteKeys.length; i++) {
Key key = new Key(whiteKeys[i], whiteNotes[i]+".wav", "white-key.png", "white-key-down.png");
addObject(key, i*63 + 54, 140);
}
// make the black keys
for(int i = 0; i < blackKeys.length; i++) {
if( ! blackKeys[i].equals("") ) {
Key key = new Key(blackKeys[i], blackNotes[i]+".wav", "black-key.png", "black-key-down.png");
addObject(key, i*63 + 85, 86);
}
}
}
}