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

2020/5/21

Printing an image in another class

SnowFlurry SnowFlurry

2020/5/21

#
I have a Text class which has the following code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Text extends Actor
{
    //private GreenfootImage box = new GreenfootImage(500, 100);
    private GreenfootImage text = new GreenfootImage(100, 100);
    private Font test = new Font("Cambria", 24);
    
    public Text(){
        setImage(text);
        //text.setTransparency(100);
    }

    public void textBox(String s, Font f, int size){
        //text.setTransparency(0);
        setLocation(250,250);
        text.setFont(f);
        text.setColor(Color.CYAN);
        text.drawString(s,50,50);
        setImage(text);
        System.out.print("hello world");
    }
    
}
And a main class that calls a Text object like the following:
private Font test = new Font("Cambria", 24);
Text t = new Text();
t.textBox("The quick brown fox jumps over the lazy dog", test, 36);
The textBox() method works when I call it from a act(0 method inside the Text class, but it won't work when called from the main class. I dragged the text class onto the world canvas when I run it, and the textBox() method does run (I determined that using the System.out.print line at the end of textBox()) but it doesn't set the image for some reason. Edit: I realised I used a private font object that's inaccessible to the main class in my post, but it doesn't work when it's another object that's accessible to the main class either. I have seen other scenarios on this site print an image using a separate class so I know it's possible to make this work, but I am not sure how others do it. Thanks in advance!
danpost danpost

2020/5/22

#
In your 3-line main class code above, line 2 creates a new Text object, apart from any you may already created and possibly placed into your world. Since this new Text object is never added to the world, you will not see its text. Any text you may already see are from different Text objects.
SnowFlurry SnowFlurry

2020/5/22

#
I see, that makes sense. But then how would I make the image set to the desired text? not placing anything on the canvas before running won't work. I checked whether the text is drawn outside the GreenfootImage box, and part of the text was drawn outside the box in my code above, but repositioning the text so that it shows up correctly doesn't fix the issue of the method not working when called from another class. Is there anything else I'm missing?
danpost danpost

2020/5/22

#
Looks like line 20 has the text starting in the middle of the image. You could use a smaller value for the first '50'.
SnowFlurry SnowFlurry

2020/5/22

#
Thanks! That does make the text print in a visible place, but it still only prints when called from the Text class, calling the textBox() method from the main class still does not make the code print the image... I'm stumped.
danpost danpost

2020/5/22

#
SnowFlurry wrote...
Thanks! That does make the text print in a visible place, but it still only prints when called from the Text class, calling the textBox() method from the main class still does not make the code print the image... I'm stumped.
Show main class codes with attempt.
You need to login to post a reply.