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

2017/12/11

I'm having trouble drawing a String and setting Fonts!

Mushi Mushi

2017/12/11

#
I have tried 2 variations of this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Label extends Actor
{
    String text = "blah, blah";
     
    public void Label(String text)
    {
        // GreenfootImage image = new GreenfootImage(200, 200);
        //image.drawString(text, 10, 16);
        GreenfootImage image = new GreenfootImage(text, 14, Color.BLACK, Color.WHITE, Color.RED);
        image.setFont(new Font("Courier New", 20));
        setImage(image);
    }
      
}
to try to create a GreenfootImage object with a font inside it that has a Courier New font. I have also tried this just to utilize the drawString() method:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class Label extends Actor
{
    String text = "blah, blah";
     
    public void Label(String text)
    {
        GreenfootImage image = new GreenfootImage(200, 200);
        image.drawString(text, 10, 16);
        // GreenfootImage image = new GreenfootImage(text, 14, Color.BLACK, Color.WHITE, Color.RED);
        image.setFont(new Font("Courier New", 20));
        setImage(image);
    }
      
}
Neither creates a text object. It's just a green foot. But the code compiles. :/ What am I doing wrong? EDIT: OH! Also, if I add another parameter to the setFont:
1
image.setFont(new Font("Courier New", Font.BOLD, 20));
It no longer compiles...?
Super_Hippo Super_Hippo

2017/12/11

#
Remove 'void'. Also changing the font only applies to drawn text after this change. This might help too: https://www.greenfoot.org/topics/60051/0
Mushi Mushi

2017/12/11

#
public Label() { String text = "blahblah"; GreenfootImage image = new GreenfootImage(200, 200); //GreenfootImage image = new GreenfootImage(text, 14, Color.BLACK, Color.WHITE, Color.RED); Font blahblah = new Font("SanSerif", 20); if (blahblah != null) { image.setFont(blahblah); } else { System.out.println("error"); } image.drawString(text, 10, 16); setImage(image); } Hey, I took out void but it still will not create the Label object?
danpost danpost

2017/12/11

#
Instead of 16 as the argument in the 'drawString' method, try a larger number (like 30, to start with). Also, make sure the actor is located no less than (100, 100) in the world.
Mushi Mushi

2017/12/11

#
EDIT: It was drawing the label, but adding the object of the label too far off the screen for me to see. Sorry!!!
You need to login to post a reply.