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

2017/1/19

Changing the font size of showtext()

BigGrosse BigGrosse

2017/1/19

#
Is it possible to change the font size of showText(java.lang.String text, int x,int y) in the world class, or do i have to replace it with an actor showing text?
danpost danpost

2017/1/19

#
BigGrosse wrote...
Is it possible to change the font size of showText(java.lang.String text, int x,int y) in the world class, or do i have to replace it with an actor showing text?
You cannot change it from within your scenarios. You would have to get the Greenfoot source and modify and repackage it. Better off just creating an Actor subclass to display the text. I usually create a simple one like this:
1
public class SimpleActor extends greenfoot.Actor {}
Yes. That is the entire class -- all that is needed to create a generic Actor object which is all you need to display text. For example, for a message display, you might have this:
1
2
3
4
5
6
7
8
9
10
11
12
13
// with an instance reference field to the actor
private Actor message = new SimpleActor();
 
// and with a method to update it image
public void setMessage(String text)
{
    GreenfootImage image = null;
    if (text != null && !"".equals(text)) image = new GreenfootImage(text, 28, null, null);
    message.setImage(image);
}
 
// with something like this line in constructor
addObject(message, 100, 20);
Then, anywhere in the class (or if accessible from outside the class), you can change the text:
1
2
3
4
5
6
7
// in class to set a message
setMessage("Hello, world!");
// in class to clear a message
setMessage(null); // or 'setMessage("");'
 
// from Actor object in a world called MyWorld
((MyWorld)getWorld()).setMessage("Hola, mundo!");
BigGrosse BigGrosse

2017/1/20

#
Okay thank you @danpost i will try it out!
You need to login to post a reply.