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

2021/5/29

How do I change font of the text?

lary_ lary_

2021/5/29

#
I tried a lot of things but nothing has worked.
Super_Hippo Super_Hippo

2021/5/29

#
Set a font, then draw the string on the GreenfootImage. You can’t change the font used by the “showText” method.
lary_ lary_

2021/5/29

#
Super_Hippo wrote...
Set a font, then draw the string on the GreenfootImage. You can’t change the font used by the “showText” method.
I did this, but it is not working.
1
2
3
getImage().setFont(new Font("Marlboro", 40));
 
setImage(new GreenfootImage(texto, 40, Color.WHITE, null));
danpost danpost

2021/5/29

#
lary_ wrote...
Super_Hippo wrote...
Set a font, then draw the string on the GreenfootImage. You can’t change the font used by the “showText” method.
I did this, but it is not working.
1
2
3
getImage().setFont(new Font("Marlboro", 40));
 
setImage(new GreenfootImage(texto, 40, Color.WHITE, null));
The image you set the font to in line one is not the image set to the actor in line 3. Use the drawString method on the image the font is set to.
lary_ lary_

2021/5/30

#
danpost wrote...
lary_ wrote...
Super_Hippo wrote...
Set a font, then draw the string on the GreenfootImage. You can’t change the font used by the “showText” method.
I did this, but it is not working.
1
2
3
getImage().setFont(new Font("Marlboro", 40));
 
setImage(new GreenfootImage(texto, 40, Color.WHITE, null));
The image you set the font to in line one is not the image set to the actor in line 3. Use the drawString method on the image the font is set to.
Thanks, it is working. Now the problem is that the font I want do not works, but when I use another like "Times new Roman" then it works :/ I do not undertand why, since the "Marlboro" font is installed in the system :/
1
2
3
4
GreenfootImage image = new GreenfootImage(200, 200);
image.setFont(new Font("Times New Roman", 40));
image.drawString(text, 30, 30);
setImage(image);
danpost danpost

2021/5/30

#
The following listFonts method (with the help of the private methods) should list all available fonts to the terminal, giving the exact names to be used:
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
public static void listFonts()
{
    Font[] fonts = convertFonts(java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts());
    int maxFonts = fonts.length;
    if (maxFonts == 0)
    {
        System.out.println("\nOnly the five Font class defined font fields are allowed.");
        String fontList = "   Font.MONOSPACED\n   Font.SANS_SERIF\n   Font.SERIF";
        fontList += "\n   Font.DIALOG\n   Font.DIALOG_INPUT";
        System.out.println(fontList);
        return;
    }
    for (int i=0; i<maxFonts; i++)
    {
        System.out.println("   ".substring((int)Math.log10(i+1))+(i+1)+") \""+fonts[i].getName()+"\"");
    }
}
 
private static Font[] convertFonts(java.awt.Font[] jfonts)
{
    Font[] fonts = new Font[jfonts.length];
    for (int i=0; i<fonts.length; i++) fonts[i] = getGFont(jfonts[i]);
    return fonts;
}
 
private static Font getGFont(java.awt.Font jfont)
{
    return new Font(jfont.getName(), jfont.getStyle()%2 == 1, jfont.getStyle()/2 == 1, jfont.getSize());
}
yatharthgaur yatharthgaur

2021/6/3

#
You can do that in power point save as png file and insert as background. Hope it helps you!
lary_ lary_

2021/6/3

#
The whole problem was the font I installed in my pc, so I have to use another one. But this is working for me.
1
2
3
4
GreenfootImage image = new GreenfootImage(200, 200);
image.setFont(new Font("Times New Roman", 40));
image.drawString(text, 30, 30);
setImage(image);
Thanks for the help, everyone.
You need to login to post a reply.