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

2012/1/22

How do I DrawString?

Razzo Razzo

2012/1/22

#
GreenfootImage bitcounter = new GreenfootImage(30, 370); bitcounter.drawString("Bits: "+bits, 0, 0); The code i'm using doesn't work, it just creates the Greenfoot icon in the required place..
Builderboy2005 Builderboy2005

2012/1/22

#
If you are trying to modify the image of an actor, make sure you use
setImage(bitcounter);
So that the image of the actor gets set to the one you are drawing to.
Razzo Razzo

2012/1/22

#
Nope.. Still nothing ..
danpost danpost

2012/1/22

#
The y coordinate of the drawString method is for the BASE-LINE of the string; change that value to at least the font-size. And if you have the greenfoot icon displayed instead of your image, you still need to set the image of the object as Builderboy2005 said.
Razzo Razzo

2012/1/22

#
    public static int bits;
    public void act() 
    {
        getImage().clear();
        getImage().drawString("Bits: "+bits, 0, 16);
    }    
Okay I used that, and it snips the interger Bits, so doesnt show it all! When I change the Numbers it does it even more
danpost danpost

2012/1/22

#
I can think of a couple ways to solve this: 1) increase the width of the object you are drawing on (so it shows it all) or, if that is not an option, 2) create a new image with 'new GreenfootImage(String text, int fontsize, Color foregroundColor, Color backgroundColor) and use drawImage(GreenfootImage imageName, int xLocation, int yLocation) to add the text to the image of the object. This way you can adjust the font-size to make it fit on the object. You will need to 'import java.awt.Color;'.
Razzo Razzo

2012/1/22

#
Okay :D Thanks, Its working now :D http://www.greenfoot.org/scenarios/4078 I made that now :D
You need to login to post a reply.