This site requires JavaScript, please enable it in your browser!
Greenfoot back
Tim1234
Tim1234 wrote ...

2013/2/26

Greenfoot chapter 5 add text

Tim1234 Tim1234

2013/2/26

#
I have finished chapter 5 of the greenfoot book. I want to add text to the keys of the piano, that says which note they play. Does anybody know how to do this?
Jonas234 Jonas234

2013/2/26

#
you can do sth. like:
getImage().setColor(new Color(255, 0, 0));
        getImage().setFont(new Font("Arial", Font.BOLD, 18));
        getImage().drawString(key,23,15);
setColor sets the color of your string with RGB. setFont lets you set a new Font first font then style and then the size. And drawString writes your String on the Image key is here your String (the text you want to write) and 23,15 the coordinates in the Picture.
Tim1234 Tim1234

2013/2/26

#
I have added a similar code to my piano, but there doesn't show up any text. This is my (partial) code:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;

/**
 * A piano that can be played with the computer keyboard.
 */
public class Piano extends World
{
private String[] whiteKeys =
        { "a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "[", "\\" };
private String[] whiteNotes =
        { "2c", "2d", "2e", "2f", "2g", "2a", "2b", "3c", "3d", "3e", "3f", "3g" };
private String[] blackKeys =
        { "w", "e", "", "t", "y", "u", "", "o", "p", "", "]" }; 
private String[] blackNotes =
        { "2c#", "2d#", "", "2f#", "2g#", "2a#", "", "3c#", "3d#", "", "3f#" }; 
private String[] keyNotes =
        { "2c", "2d", "2e", "2f", "2g", "2a", "2b", "3c", "3d", "3e", "3f", "3g" };
                 
    /**
     * 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();
        showMessage();         
    }
    
    /**
     * Create the piano keys and place them in the world.
     */
    public 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, 54 + (i*63), 140);
            GreenfootImage bg = getBackground();
            bg.setColor(Color.BLACK);
            bg.drawString(keyNotes[i], 23, 15);
        }
Jonas234 Jonas234

2013/2/26

#
i am not sure but it could be that the actors are abvoe your strings. You could try: key.drawString(keyNotes, 23, 15); this instead.
Tim1234 Tim1234

2013/2/26

#
Doesn't seem to work. Thanks for helping anyway.
Jonas234 Jonas234

2013/2/26

#
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, 54 + (i*63), 140);
            key.getImage().setColor(new Color(255, 0, 0));  
                key.getImage().setFont(new Font("Arial", Font.BOLD, 18));
                key.getImage().drawString(whiteNotes[i],15,200);
        }
Ok tried it myself. This should be working.
Tim1234 Tim1234

2013/2/26

#
It's working now. Thanks.
You need to login to post a reply.