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

2016/4/15

Label size

zobear888 zobear888

2016/4/15

#
I am using this code to change my label's size but nothing happens: public void act() { getImage().setFont(new java.awt.Font("Helvetica", Font.PLAIN, 50)); } this is the creation code for the label if that helps: public Label(String text) { GreenfootImage img = new GreenfootImage (text.length()*20, 30); img.drawString (text, 4, 25); setImage (img); }
danpost danpost

2016/4/15

#
Setting the font only sets the font for future text drawing methods. It does not change the font of the text that is already there.
zobear888 zobear888

2016/4/15

#
Thanks! How would I do that?
danpost danpost

2016/4/15

#
Please read this in its entirety before changing your code. Currently, there is no way for the act method to know what text was drawn on the image of the actor. You will need to add an instance String field to hold the text so that it can be saved until you need it in the act method:
// add new instance field
private String caption; // holds the drawn text

// add this line in the constructor
caption = text; // saves the drawn text

// in act method after setting font
getImage().clear(); // removes unmodified text
getImage().drawstring(caption, 4, 25); // redraws the text
Now, I noticed that this changing of font is performed unconditionally within the act method -- meaning that you may not want the original font to be shown at all. If this is the case then all you need to do is move the one line in your act method to after the line that creates the image in the constructor:
public Label(String text)
{
    GreenfootImage img = new GreenfootImage(text.length()*20, 30);
    img.setFont(new java.awt.Font("Helvetica", java.awt.Font.PLAIN, 50));
    img.drawString(text, 4, 25);
    setImage(img);
}
zobear888 zobear888

2016/4/22

#
Thank You!Here is the url to my finished game! http://www.greenfoot.org/scenarios/16534
You need to login to post a reply.