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

2017/12/10

How do i change the font?

Recorsi Recorsi

2017/12/10

#
Hi, i made some sort of game over screen, it works but i want to change the font. how do i do that? code of the game over class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Font;
/**
 * You died.
 *
 * @author
 * @version 8.12.17
 */
public class DeathScreen extends Actor
{
    public DeathScreen()
    {
        setImage(new GreenfootImage("YOU DIED", 100, Color.RED, Color.BLACK));
         
         
         
         
         
        //setFont(new Font("OptimusPrinceps", Font.PLAIN, 20));
    }
}
i tried some things with "setFont" but i cant get it to work thanks :)
danpost danpost

2017/12/10

#
Recorsi wrote...
< Code Omitted > i tried some things with "setFont" but i cant get it to work
Remove line 2 and change 'Font.PLAIN' to 'false, false'. Greenfoot uses its own Font class now.
Recorsi Recorsi

2017/12/11

#
It still doesn't like the "setFont" method. What is missing here?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * You died.
 *
 * @author
 * @version 8.12.17
 */
public class DeathScreen extends Actor
{
    public DeathScreen()
    {
        setImage(new GreenfootImage("YOU DIED", 100, Color.RED, Color.BLACK));
        setFont(new Font("OptimusPrinceps", false, false , 20));
    }
}
danpost danpost

2017/12/11

#
You cannot set the font of an actor (your DeathScreen actor, in this case). A GreenfootImage object is what that action can be performed on. Use the 'getImage' method on the actor to get the image to set the font on. However, this will not change the image in itself. The text was already placed on the image using a default font when you created the image. The 'setFont' method only changes how text is subsequently drawn on the image.
Recorsi Recorsi

2017/12/11

#
How do i change the font of the text then?
Super_Hippo Super_Hippo

2017/12/11

#
Create an image. Set a font and then, draw a string on the object. You can not use this constructor when you don't want the default font.
1
2
3
4
5
6
7
GreenfootImage img = new GreenfootImage(500, 100);
img.setColor(Color.BLACK);
img.fill();
img.setColor(Color.RED);
img.setFont(new Font("OptimusPrinceps", false, false , 100));
img.drawString("YOU DIED", 10, 90);
setImage(img);
bromung bromung

2017/12/11

#
is it possible change the size of the text?
Super_Hippo Super_Hippo

2017/12/11

#
The 100 in line 5 is the font size.
Recorsi Recorsi

2017/12/12

#
Super_Hippo wrote...
Create an image. Set a font and then, draw a string on the object. You can not use this constructor when you don't want the default font.
1
2
3
4
5
6
7
GreenfootImage img = new GreenfootImage(500, 100);
img.setColor(Color.BLACK);
img.fill();
img.setColor(Color.RED);
img.setFont(new Font("OptimusPrinceps", false, false , 100));
img.drawString("YOU DIED", 10, 90);
setImage(img);
Thanks :)
You need to login to post a reply.