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

2025/6/10

How to make text appear

Philipe_Manor Philipe_Manor

2025/6/10

#
Hello friends, how do I make text appear on my project?
danpost danpost

2025/6/11

#
Philipe_Manor wrote...
Hello friends, how do I make text appear on my project?
The quick an easy way is using the World instance method of showText(String, int, int) method. However, it is very limited as to size, font and color (only one). To open up different sizes, fonts and colors, you would need to create an actor and draw a GreenfootImage of a String onto its image: With an Actor subclass of:
public class Aktor extends greenfoot.Actor {}
(yes, that is the entire class code) This is so you can create an actor with no additional behaviors or states. Then, simply
String text = "Whatever text"; // site a text string
Actor actor = new Aktor(); // create an actor
GreenfootImage txtImg = new GreenfootImage(text 26, Color.BLACK, new Color(0, 0, 0, 0)); // create image of text string
actor.setImage(txtImg); // give actor its image
addObject(actor, 120, 24); // add actor to world
You could even make a method out of it if you need to do this multiple times:
private Actor getBasicTextActor(String text, int size, Color textColor) {
    Actor actor = new Aktor();
    GreenfootImage textImage = new GreenfootImage(text, size, textColor, new Color(0, 0, 0, 0));
    actor.setImage(textImage)
    return actor;
}
Usage:
String text = "Whatever text";
addObject(getBasicTextActor(text, 26, Color.BLACK), 120, 24);
You need to login to post a reply.