I have a Text class which has the following code:
And a main class that calls a Text object like the following:
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!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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" ); } } |
1 2 3 | private Font test = new Font( "Cambria" , 24 ); Text t = new Text(); t.textBox( "The quick brown fox jumps over the lazy dog" , test, 36 ); |