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

2012/5/30

Draw String

thefutureisnow thefutureisnow

2012/5/30

#
Yeah, still having problems with this. Could someone reply with the code of how I would place a text into my world using the "drawString" command, and tell me if it's in the world class, or an actor, or what. Thanks!
MatheMagician MatheMagician

2012/5/30

#
If you don't care what size of text you are using then just import java.awt.Color and say
getBackground().setColor(Color.red);//or another color
        getBackground().drawString("this is how you do it", 200, 300);
Otherwise you also need to import java.awt.Font and put this code in front of the last bit.
Font font = getBackground().getFont();
        font = font.deriveFont(14.0f);
        getBackground().setFont(font);
This code is designed for the word class. If you want to use it in the actor class, replace all instances of getBackground() with getImage().
ttamasu ttamasu

2012/5/30

#
drawString is part of the GreenfootImage class and so any reference you get like getImage(), unlike an actor where the x and y is centered at the center of the image drawstring is at the leftmost, baseline of the string. The color of the string is your current color set by the setColor command and it writes on top of the current image. If you want a multi string text I would just use the GreenfootImage object to create it. eg. // from the world class you can ... String text = "Hello World \n Greenfoot"); // Greenfoot is on second line // Image of Text is 18 font size, White Text, Blue background GreenfootImage ImageText = new GreenfootImage(text, 18, Color.WHITE,Color.BLUE); GreenfootImage bg = getBackGround(); bg.drawImage(ImageText, 1,1); // upper left corner of image is at 1,1.
thefutureisnow thefutureisnow

2012/5/30

#
When I put this code in the actor script, should I put the actor into the "prepare()" method? or will it show on its own?
thefutureisnow thefutureisnow

2012/5/30

#
When I use the draw something in an actor, it doesn't seem to work. I did what you said, to change the getBackground() to getImage(), but it still doesn't work. It doesn't seem to give me any error, and I put the actor in the prepare() in the world. All I see though, is the greenfoot symbol, which is default for actors without images.
matt.milan matt.milan

2012/5/30

#
I had a similar problem today. Make sure you're using setImage(image) in your code. if it's there, and in the right spot, and it still doesnt work. let us know i like to make an updateImage() method that i call first from the constructor, and then any time i change the string i start the method with img.clear() to get rid of the old string, and then finish with setImage(img)
You need to login to post a reply.