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

2015/7/2

Warpping text

wabuilderman wabuilderman

2015/7/2

#
I don't know how to do this strangely enough. I have a name/story generator that I am working on, but I need to have the text wrap. Anybody know how to do this? here is my current code (minus the generation algorithms):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
String Homeland = newName("");
        String Hero = newName("");
        String Family = newName("");
        String Season = newName("");
        String LegendarySword = newName("");
        String LegendaryBeast = newName("");
         
         
        String txt = "In the land of " + Homeland + ", there was a Hero. His name, " + Hero + ", was that of the " + Family +
        " Clan. They were known for defeating the great " + LegendaryBeast + " who had threatened the entire land. " + Hero + " recieved the ancient blade " +
        LegendarySword + " when he turned eighteen. This was a time of merryment and celebration, but it was not so. That evening, the great beast, " + LegendaryBeast
        + ", rose from the forest. It had returned, and sought vengence on the family who had smited it so long ago.";
        GreenfootImage textImage = new GreenfootImage(txt, 24, new Color(208, 191, 146), new Color(0, 0, 0, 0));
        GreenfootImage image = new GreenfootImage(textImage.getWidth()+12, 36);
        image.setColor(new Color(0, 0, 0));
        image.fill();
        image.setColor(new Color(158, 141, 96));
        image.fillRect(3, 3, image.getWidth()-6, 30);
        image.drawImage(textImage, 6, 6);
        this.setImage(image);
This is all carried out fine, but the text goes off the screen.
davmac davmac

2015/7/2

#
Maybe I don't understand the question, but this should be easy enough - just split the string if it gets too long, and generate two or more separate GreenfootImages for each line after the split. If you wanted to you could re-combine the lines into a single image by painting each line onto a new "canvas" image.
wabuilderman wabuilderman

2015/7/3

#
Ok, Its just that I don't know any way to split it at the end of words, not the exact letter. Also, I believe I could just use \n to break them up, but the problem is finding where to insert it.
David-FireTech David-FireTech

2015/7/3

#
Can you do REGEX to look for a space after a certain number of characters?
danpost danpost

2015/7/3

#
David-FireTech wrote...
Can you do REGEX to look for a space after a certain number of characters?
Character glyphs are not all the same width; so, you would have to go with the limit on the widest glyph -- and then the resultant size of the lines may vary too much to look normal (you could end up with a lot of empty space at the end of one line with several short words at the beginning of the next). I would suggest using the 'split' method of the String class to break the text into words and rebuild the text by limiting the width of the image created from it -- before the word being tested, either insert the space back in (if the image is still within limits) or insert a new line escape sequence, a backslash followed by an 'n' (if the image is outside the limits). Once the line breaks are in place, you can build the final image line by line by first splitting the new text into lines, creating a black image for the line images to be drawn on and then creating the image for each line and drawing it on blank image. It would be something like this:
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
26
27
28
29
30
// the fields
int width = 600; // maximum width of a line
int size = 24; // font size of text
int height = new GreenfootImage(" ", size, null, null).getHeight();
// the text
String text = "Import my TextImage support class into your project and you ";
text += "will be able to easily make text images of any available font, ";
text += "style, colors and size just by creating TextImage objects instead of ";
text += "GreenfootImage objects.\n \nYou need to download and open the demo in ";
text += "greenfoot and then either copy/paste the code from the TextImage class ";
text += "into a new class in your project or copy/paste the 'TextImage.java' ";
text += "file itself into the 'Greenfoot/lib/greenfoot/common' folder so you can ";
text += "'Edit>Import class...' from the menubar in the greenfoot application. ";
 
// finding the breakpoints
String[] words = text.split(" "); // get words
text = words[0]; // start rebuilding text
for (int i=1; i<words.length; i++)
{ // insert appropriate character(s) between words
    int w = new GreenfootImage(text+" "+words[i], size, null, null).getWidth(); // width of line with the next word
    text += (w > width-20 ? "\n" : " ")+words[i];
}
// building the image
String[] lines = text.split("\n"); // get lines
GreenfootImage image = new GreenfootImage(width, height*lines.length); // final image
for (int i=0; i<lines.length; i++)
{ // draw each line image on final image
    GreenfootImage line = new GreenfootImage(lines[i], size, null, null);
    image.drawImage(line, 10, i*height);
}
The values of 'width' and 'size' can be changed as desired (as can 'text', of course). The final desired text image is in the object reference variable 'image'. The colors can be added in as needed (I coded the above for black text on a transparent background).
You need to login to post a reply.