As the title says, I am having trouble getting the black keys space evenly without hard coding it. If anyone could help it would be greatly apreciated.
Here is my code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | 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(); createMessage(); } /** * Create the piano keys and place them in the world. */ private void makeKeys() { 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 *key.getImage().getWidth() + key.getImage().getWidth(), key.getImage().getHeight() / 2 ); } for ( int i = 0 ; i < whiteKeys.length- 1 ; i++) { if ( ! blackKeys[i].equals( "" ) ) { Key key = new Key(blackKeys[i], blackNotes[i]+ ".wav" , "black-key.png" , "black-key-down.png" ); addObject(key, i *key.getImage().getWidth() + key.getImage().getWidth() * 2 , key.getImage().getHeight() / 2 ); } } } public void createMessage() { GreenfootImage bg = getBackground(); bg.drawString( "Use keys 'a' to ':' and keys 'q', 'e', 't', 'y', 'u', 'o', 'p', ] to play the piano" , 225 , 300 ); } } |