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

2017/5/3

Why doesn't this code work?

1
2
Wasupmacuz Wasupmacuz

2017/5/3

#
I have code to create text in the world but when it is created, it's invisible. Code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;
/**
 * Write a description of class PlainText here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PlainText extends Actor
{
    
    public PlainText(String text,int x,int y)
    {
        Font f = new Font("Times New Roman", Font.ITALIC, 11);
        GreenfootImage gi= new GreenfootImage(300,300);
        gi.setFont(f);
        gi.setColor(Color.WHITE);
        gi.drawString(text,x,y);
        setImage(gi);
    }
}
Thanks in advance!
Yehuda Yehuda

2017/5/3

#
From what I see it seems that you're writing text just it's in white so if you're placing this actor on top of something white then you won't see it. If you change the color on line 18 to something like Color.GREEN, then you might see it (unless your background is green).
danpost danpost

2017/5/3

#
What color is your world background? What code are you using to create and add a PlainText object into the world? Why are you using such a large image (300x300) for small (fontsize=11) text?
Wasupmacuz Wasupmacuz

2017/5/3

#
danpost wrote...
What color is your world background? What code are you using to create and add a PlainText object into the world? Why are you using such a large image (300x300) for small (fontsize=11) text?
The color of my background is a dark grey. The code used to add the PlainText to the world is:
int statY=70;
for(int i=0;i<stats.length;i++)
{
        PlainText pt=new PlainText(stats[i],300,statY); //stats[] is a String array with things such as "Health" and "Hunger"
        getWorld().addObject(pt,300,statY);

        statY+=30;
}
I have no idea why the image is so large. (changing that now) EDIT: It is in the world. Just the image is not showing up, I suppose.
danpost danpost

2017/5/3

#
Try changing the '300' in line 4 to '0'. You were drawing the strings off the right edge of the image.
Wasupmacuz Wasupmacuz

2017/5/3

#
danpost wrote...
Try changing the '300' in line 4 to '0'. You were drawing the strings off the right edge of the image.
Thanks that fixed the issue...on one text. There are three more that do not show up with the same code. I have no idea about what could be wrong.
Yehuda Yehuda

2017/5/4

#
Since you are creating a new PlainText for each text that you want to display I don't see why you need the x,y parameters in PlainText. You should just always have the drawString method draw at 0,0. If you want to keep the parameters then you still don't need to have 'statY' as an argument on line 4, the y coordinate can also be 0. On line 5 you can change the 'statY' argument for the addObject method to:
getWorld().addObject(pt, 300, 70 + i * 30);
So basically your loop can simply be:
for (int i = 0; i < stats.length; i++) {
    getWorld().addObject(new PlainText(stats[i], 0, 0), 300, 70 + i * 30);
    // as mentioned before since the arguments for PlainText can always be 0, you don't need the parameters
}
Wasupmacuz Wasupmacuz

2017/5/4

#
Yehuda wrote...
Since you are creating a new PlainText for each text that you want to display I don't see why you need the x,y parameters in PlainText. You should just always have the drawString method draw at 0,0. If you want to keep the parameters then you still don't need to have 'statY' as an argument on line 4, the y coordinate can also be 0. On line 5 you can change the 'statY' argument for the addObject method to:
getWorld().addObject(pt, 300, 70 + i * 30);
So basically your loop can simply be:
for (int i = 0; i < stats.length; i++) {
    getWorld().addObject(new PlainText(stats[i], 0, 0), 300, 70 + i * 30);
    // as mentioned before since the arguments for PlainText can always be 0, you don't need the parameters
}
Thank you. I simplified it a little bit but kept it kind of like I had it before (so my small mind can still understand it :D )
public void openStatMenu()
    {
        StatMenu sm=new StatMenu();
        getWorld().addObject(sm,300,200);
        Eat eat=new Eat();
        getWorld().addObject(eat,312,330);
        for(int i=0;i<stats.length;i++)
        {
            PlainText pt=new PlainText(stats[i],70+i*30);
            getWorld().addObject(pt,300,70+i*30);
        }
    }
Yehuda Yehuda

2017/5/4

#
If that's what you did AND you don't get any errors then what is the current constructor for PlainText? You did: PlainText(string, int) on line 9, you shouldn't need any ints at all. I don't think the following is very clear: The coordinates with which you add the PlainText to the world don't have anything to do with the coordinates with which you draw onto the GreenfootImage in PlainText. You have a blank image in PlainText and on that you draw text. If you're only using this class to add some small text to the world, then you should just do:
gi.drawString(text, 0, 0);
Then add the PlainText object into the world at the specified coordinates. Don't mix up the addObject coordinates with the drawString coordinates. The PlainText class should just be:
import greenfoot.Actor;
import greenfoot.GreenfootImage;
import java.awt.Color;
import java.awt.Font;

/**
 * Write a description of class PlainText here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class PlainText extends Actor {

    public PlainText() {
        this("");
    }

    public PlainText(String text) {
        GreenfootImage gi = new GreenfootImage(300, 20);
        gi.setFont(new Font("Times New Roman", Font.ITALIC, 11));
        gi.setColor(Color.WHITE);
        gi.drawString(text, 0, 0);
        setImage(gi);
    }
}
And line 9 in your above code should be:
PlainText pt = new PlainText(stats[i]);
Wasupmacuz Wasupmacuz

2017/5/4

#
Yehuda wrote...
If that's what you did AND you don't get any errors then what is the current constructor for PlainText? You did: PlainText(string, int) on line 9, you shouldn't need any ints at all. I don't think the following is very clear: The coordinates with which you add the PlainText to the world don't have anything to do with the coordinates with which you draw onto the GreenfootImage in PlainText. You have a blank image in PlainText and on that you draw text. If you're only using this class to add some small text to the world, then you should just do:
gi.drawString(text, 0, 0);
Then add the PlainText object into the world at the specified coordinates. Don't mix up the addObject coordinates with the drawString coordinates. The PlainText class should just be:
import greenfoot.Actor;
import greenfoot.GreenfootImage;
import java.awt.Color;
import java.awt.Font;

/**
 * Write a description of class PlainText here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class PlainText extends Actor {

    public PlainText() {
        this("");
    }

    public PlainText(String text) {
        GreenfootImage gi = new GreenfootImage(300, 20);
        gi.setFont(new Font("Times New Roman", Font.ITALIC, 11));
        gi.setColor(Color.WHITE);
        gi.drawString(text, 0, 0);
        setImage(gi);
    }
}
And line 9 in your above code should be:
PlainText pt = new PlainText(stats[i]);
Thanks but this still does not fix the issue of the images not showing up. EDIT: this actually made things worse :(
Wasupmacuz Wasupmacuz

2017/5/4

#
A screenshot of the project here The white scribbles in the middle are the PlainTexts
danpost danpost

2017/5/4

#
Change line 22 in Yehuda's code to:
gi.drawString(text, 0, 16);
Hopefully, that will help.
Wasupmacuz Wasupmacuz

2017/5/4

#
danpost wrote...
Change line 22 in Yehuda's code to:
gi.drawString(text, 0, 16);
Hopefully, that will help.
Thank you Yehuda and Dan! It works!
Wasupmacuz Wasupmacuz

2017/5/5

#
Dan, I have made another class that uses the drawString method and it works now but I had to use ("string", x, 16) like you told me in order to get them to work as well. However, I'm still not sure why I need to do this. Would you happen to know?
Yehuda Yehuda

2017/5/5

#
The coordinates for the drawString method are that the X puts the left side of the first letter at the specified X, but the Y puts the bottom of the text as the specified Y so since your text is 11 pixels high you have to set the Y for drawString as at least 11.
There are more replies on the next page.
1
2