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

2017/1/17

Text Alignment on New Line

Theangryman Theangryman

2017/1/17

#
Hey guys, me again with the same program as before, but this time the issue I've got is less logical and more visual. I've got a section on-screen that requires a string to be broken up into multiple lines using "\n", however, using "\n" centres the new line below the old one, instead of lining it up to the left-hand edge of the image used to specify the text, colour and size of the string.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private String descriptionText;
 
    public DescBox(String descText)
    {
        descriptionText = descText;
        descriptionBox();
    }
    public void act()
    {
        descriptionBox();
    }
    public void descriptionBox()
    {
        GreenfootImage descBg = new GreenfootImage(455, 100);
        descBg.setColor(new Color(75, 75, 75, 200));
        descBg.fillRect(0, 0, 455, 100);
        GreenfootImage partDescText = new GreenfootImage(descriptionText, 18, Color.WHITE, null);
        descBg.drawImage(partDescText, 5, 5);
        setImage(descBg);
         
    }
The "descriptionText" string being parsed comes with the "\n" escape character already inserted into it at the correct places for formatting the string, but currently the example text used for testing purposes uses characters of equal width between each "\n" so the problem is mostly hidden right now. Anybody know how to stop the unwanted centring?
Super_Hippo Super_Hippo

2017/1/17

#
I don't know if there is a more easier way, but I would probably create a new image for each line and then draw all these images below each other on one image which the object can use.
You need to login to post a reply.