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

2014/9/28

Overlaying a String onto an image

xChaoswar xChaoswar

2014/9/28

#
How can I create a string in the same class as the object and have it overlay it's preset image. I want to be able to change this value as well and have it update on the screen.
Super_Hippo Super_Hippo

2014/9/28

#
What you might want is the 'drawString' method. With this, you can draw a string on a GreenfootImage object.
xChaoswar xChaoswar

2014/9/28

#
Could you please give me an example?
xChaoswar xChaoswar

2014/9/28

#
I've tried this but only the PNG shows up.
public Button()
    {
        GreenfootImage backImage = new GreenfootImage("Button.png");
        
        GreenfootImage text = new GreenfootImage(445,390);
        text.setColor(Color.CYAN);
        text.drawString("Words",200,200);
        text.scale(900,780);
        backImage.drawImage(text,3,2);
        setImage(backImage);
    }
Super_Hippo Super_Hippo

2014/9/28

#
Hm, I think you can just use 'backImage.drawString'. Why do you scale the 'text' Image?
xChaoswar xChaoswar

2014/9/28

#
Just to make the text larger. Though there's probably an easier way. And Nothing happens if I remove the setImage code.
danpost danpost

2014/9/28

#
I think because you have an abundance of transparency around the drawn string that when you draw the image on the button image, you are only drawing an area of transparency. A better way might be to use the GreenfootImage constructor that creates a textual image of given size and draw it on your button image, replacing lines 5 through 9 with this:
GreenfootImage text = new GreenfootImage("Words", 24, Color.CYAN, null);
backImage.drawImage(text, 3, 2);
xChaoswar xChaoswar

2014/9/29

#
It works now! Thank you!
You need to login to post a reply.