1

I am making a little game which has a text box. The text box will draw a string which will separate into multiple lines to fit into the text box. I am having a little bit of trouble with the "separating into multiple lines" part.

String[] line = new String[4];
int boxWidth = 200;
String MESSAGE = "This is a long message to make sure that the line separation works.";
String[] splitArray = null;

try {
    splitArray = MESSAGE.split("\\s+"); //split the words of the sentence into an array
} catch (PatternSyntaxException e) {
    CrashDumping.DumpCrash(e);
}

String cursentence = "";
int curleng = 0;
int curline = 0;
for (int i = 0; i < splitArray.length; i++) {
    if (curleng + m.getStringWidth(splitArray[i], r.font1) <= boxWidth) {
        curleng += m.getStringWidth(splitArray[i], r.font1);
        cursentence += splitArray[i]; cursentence += " ";
    } else {
        line[curline] = cursentence;
        cursentence = "";
        curline++;
        curleng = 0;
    }
}
for (int i = 0; i < line.length; i++) {
    System.out.println("Line " + i + " - " + line[i]);
}

int getStringWidth(String sentence, Font font) is a method I wrote which return the string width in pixels. This method works; it is not the problem.

public int getStringWidth(String text, Font font) {
    AffineTransform affinetransform = new AffineTransform();
    FontRenderContext frc = new FontRenderContext(affinetransform,true,true);
    return (int)(font.getStringBounds(text, frc).getWidth());
}

The output should look something like this:

Line 0 - This is a long message to make sure 
Line 1 - that the line separation works.
Line 2 - null
Line 3 - null

But will only print out the first line, the last 3 are just null. So for some reason the for loop is breaking after finishing the first line.

What is going on here?

4
  • Are you looking for something like stackoverflow.com/questions/4212675/…? Commented Mar 22, 2015 at 23:32
  • @Pshemo no not quite, because if I understood what that question was wanting was just to print it out and i need to store the strings in variables to access them when i render them later on, the printing is just for debuging. Commented Mar 22, 2015 at 23:39
  • You have to concatenate the current word in the 'else' branch too. Commented Mar 22, 2015 at 23:46
  • OK, i did that and things seem to be working well. Thank you! Commented Mar 23, 2015 at 1:18

1 Answer 1

2

You assign line[curline] = cursentence;only when your current line is full. But you don't make assignment after the for loops finishes, meaning your remainder when you run out of words gets lost. Add this block of code under you for loop:

if (!"".equals(cursentence)) {
    line[curline] = cursentence;
}

As @rodrigoap said, you also need to start your new line with the word that didn't fit in the first line, which goeas into else block instead of cursentence = "";

cursentence = splitArray[i];
Sign up to request clarification or add additional context in comments.

2 Comments

would I put the line of code bellow my else statement?
Never mind, using that line of code and adding what @rodrigoap said worked out. Thanks for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.