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

2024/6/10

Text gains double spacing when uploaded to website

Owlias Owlias

2024/6/10

#
Upon uploading my project (linked here ), all of my text within my textbox gains a double spacing that was not present within the local version made on my computer. This is the code for TextBox:
import greenfoot.*;


public class TextBox extends Actor
{
    private String text;
    private int width;
    private int height;
    private Color backgroundColor;
    private Color textColor;
    private String line1 = "";
    private String line2 = "";
    private String line3 = "";
    private String line4 = "";
    private String line5 = "";

    public TextBox(String text, int width, int height, Color backgroundColor, Color textColor)
    {
        this.text = text;
        this.width = width;
        this.height = height;
        this.backgroundColor = backgroundColor;
        this.textColor = textColor;
        updateImage();
    }
    //Updates image
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(width, height);
        image.setFont(new Font("Arial", 10));
        image.setColor(backgroundColor);
        image.fillRect(0, 0, width, height);
        image.setColor(textColor);
        //Adjust below if needed (width and height)
        image.drawString(text, 10, 10);
        setImage(image);
    }
    //Sets the text that is displayed in the textbox
    public void setText(String newText)
    {
        if(newText.equals("")) {
            this.text = "";
        }
        else {
            line1 = line2;
            line2 = line3;
            line3 = line4;
            line4 = line5;
            line5 = newText;
            this.text = line1 + "\n" + line2 + "\n" + line3 + "\n" + line4 + "\n" + line5;
        }
        updateImage();
    }
    public String getText() {
        return text;
    }
}
Any suggestions on how to stop the double spacing or is this just inevitable?
danpost danpost

2024/6/11

#
Owlias wrote...
Upon uploading my project (linked here ), all of my text within my textbox gains a double spacing that was not present within the local version made on my computer. This is the code for TextBox: << Code Omitted >> Any suggestions on how to stop the double spacing or is this just inevitable?
Maybe change the way the image is rendered for a TextBox instance. By drawing each line specifically where you want, you might gain control.
Owlias Owlias

2024/6/11

#
Hm alright. I ended up just leaving it as a 4 line textbox instead of 5 (changed how a lot of the text worked but eh it's ok). I also ran into an issue of one of my sprites (Goblin) doesn't get imported from the files and assigned directly to the actor. I've checked multiple times and it appears to be in there on my version (locally). Any suggestion on getting this image to be assigned? (image is naturally assigned to the actor within Greenfoot app)
danpost danpost

2024/6/13

#
Owlias wrote...
one of my sprites (Goblin) doesn't get imported from the files and assigned directly to the actor. I've checked multiple times and it appears to be in there on my version (locally). Any suggestion on getting this image to be assigned? (image is naturally assigned to the actor within Greenfoot app)
You can just set the image in the constructor of the class. For example:
public Goblin() {
    setImage("goblin.png");
}
You need to login to post a reply.