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

2022/3/6

Change the font of my text

TheMuncher TheMuncher

2022/3/6

#
I currently have this piece of code, however, whatever font name I input into the the font line, the text still looks the same
public void act()
    {
        GreenfootImage img = new GreenfootImage(500, 100);
        img.setColor(Color.BLACK);
        img.fill();
        img.setColor(Color.RED);
        img.setFont(new Font("Arial", false, false , 100)); //font line
        img.drawString("YOU DIED", 10, 90);
        setImage(img);
    }
Roshan123 Roshan123

2022/3/6

#
Just have a look on this scenario. I have used setFont() method in it. But as you can see setFont() method only works on greenfoot app and not in website
Roshan123 Roshan123

2022/3/6

#
Forget about the link. I haven't provided the source code. Here is how to use setFont() method
import greenfoot.*; 
public class Font extends Actor
{
    GreenfootImage img;
    String text;
    boolean italic, bold;
    int size;
    Color color;
    public Font(String text,Color color,boolean bold, boolean italic, int size)
    {
        this.text=text;this.color=color; this.bold=bold; this.italic=italic; this.size=size;
    }
    public void act() 
    {
        MyWorld w = (MyWorld) getWorld();
        img=new GreenfootImage(text.length()*size,size);
        img.setColor(color);
        img.setFont(new Font(text,italic,bold,size));
        img.drawString(text, getImage().getWidth()/2-text.length()-10, getImage().getHeight()/2+size-10);
        setImage(img);
    }    
}
The 1st parameter in setFont() method represents the text/message you want to add. The 2nd one represents if it should be italic or not. The 3rd one represents if it should be bold or not and the 4th one is for size as you know You can only use setFont() method for italic and bold text. Now you can use add this class as many times as you want, with different text, color, font and size
TheMuncher TheMuncher

2022/3/7

#
Thanks Roshan123
You need to login to post a reply.