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

2016/9/26

changing text size in world

FaintingSatyr FaintingSatyr

2016/9/26

#
I've succeeded in showing the letters on the keys of the piano, but I want to make the font a bit bigger.
import greenfoot.*;  // (World, Actor, [code]import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.*;
import java.lang.*;
/**
 * A piano that can be played with the computer keyboard.
 * 
 * @author: M. Kolling
 * @version: 0.4
 */
public class Piano extends World
{
    private String[] whiteKeys =
        {"z", "x", "c", "v", "b", "n", "m", "s", "d", "f", "g", "h", "j", "k"};
    private String[] whiteNotes =
        {"2a", "2b", "3c", "3d", "3e", "3f", "3g", "3a", "3b", "4c", "4d", "4e", "4f", "4g" };
    private String[] blackKeys =
        { "`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=" };
    private String[] blackNotes =
        { "2a#", "", "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(927, 340, 1);
        makeKeys();
    }
    
    /**
     * 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");
            addObject(key, 54 + (i*63), 140);
            //showText(whiteKeys[i],54 + (i*63), 250);
        }
        for(int i = 0; i < whiteKeys.length-1; i++) 
        {
            if(blackKeys[i] == "1" || blackKeys[i]== "4" || blackKeys[i] =="8" || blackKeys[i] =="-")
            {
                i++;
            }
            blackKey key = new blackKey(blackKeys[i], blackNotes[i] + ".wav");
            addObject(key, 85 + (i*63), 86);
            showText(blackKeys[i], 85 + (i*63), 140);
        }
    }
}
danpost danpost

2016/9/26

#
You cannot adjust the size of the text that 'showText' uses. You can have the keys place its keyboard key value on themselves within the constructor of the Key class. There, you can create an image with text of any size and draw it onto the image of the key. You would be creating an object of and using methods of the GreenfootImage class (see documentation).
You need to login to post a reply.