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

2014/4/25

Text with background

Unicus Unicus

2014/4/25

#
Hello again, This time, I'm having problems with texts on background, and wanted to know if it's possible to add new lines whenever needed? Something like this:
public void Title()
    {
        setImage("fltroad.png");  
        GreenfootImage title = getImage();
        title.drawString("First Line \n" + 
        "Second Line\n" + 
        "Third line\n" +
        "Fourth line", 5, getImage().getHeight()/2);
    }
Currently, all it does is write it like this:
First lineSecond lineThird lineFourth line
while I need it like this:
First line Second line Third line Fourth line
I was thinking to add a new text, and space it vertically between them, but surely there must be an easier option! Thanks in advance!!!
danpost danpost

2014/4/25

#
As an alternative to 'drawString' (since it does not appear to support formatting characters) is to use 'drawImage' and use the GreenfootImage constructor 'GreenfootImage(String, int, Color, Color)' which does support formatting characters:
String text = "First Line\nSecond Line\nThird line\nFourth line";
GreenfootImage textImg = new GreenfootImage(text, 16, java.awt.Color.black, null);
title.drawImage(textImg, 5, getImage().getHeight()/2-textImg.getHeight());
Unicus Unicus

2014/4/26

#
Thank you very much! (again)
You need to login to post a reply.