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

2013/6/14

Drawing Strings

Animateur Animateur

2013/6/14

#
Hi, I've got a problem with drawing Strings. I made a class called "Grafik" (I'm German) and made a String parameter in the constructor.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Grafik extends Actor
{
    public int x = this.getX();
    public int y = this.getY();
    public String text;

    public Grafik(String text)
    {
        getImage().clear();
        getImage().drawString(text, x, y);
    }

    public void act() 
    {
        
    } 

}
Can anyone find the mistake?
Animateur Animateur

2013/6/14

#
The error is: java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
Game/maniac Game/maniac

2013/6/14

#
you do not need the variables x and y, the x and y. Also the image wont be the right size for the text. So instead of:
public class Grafik extends Actor  
{  
    public int x = this.getX();  
    public int y = this.getY();  
    public String text;  
  
    public Grafik(String text)  
    {  
        getImage().clear();  
        getImage().drawString(text, x, y);  
    }  
  
    public void act()   
    {  
          
    }   
  
}  
do:
import greenfoot.*;
import java.awt.Color;

public class Grafik extends Actor  
{
  
    public Grafik(String text)  
    {  
        GreenfootImage img = new GreenfootImage(text, 20, new Color(0, 0, 0), new Color(0, 0, 0, 0));
    }
  
}  
Animateur Animateur

2013/6/14

#
Now there's no error, but it's just the standard greenfoot image if I create a new one
danpost danpost

2013/6/14

#
The 'img' created still needs to be set as the image of the actor. Try this:
import greenfoot.*;
import java.awt.Color;

public class Grafik extends Actor  
{
    public Grafik(String text)  
    {  
        GreenfootImage img = new GreenfootImage(text, 20, new Color(0, 0, 0), new Color(0, 0, 0, 0));
        setImage(img); // add this line
    }  
}
You need to login to post a reply.