Java wrapText in graphics2D
Currently, I am going through a 2D game design tutorial by RyiSnow on Youtube. The tutorial is good though, at times I throw in my own changes, and will likely rewrite everything at the end to adjust how it is organized. One of the changes I implemented is a word wrap static function.

I'm not generally a Java programmer, but I do like to do tutorials every once in a while to keep my skill up. Sometimes basic tutorials, sometimes advanced.
Currently, I am going through a 2D game design tutorial by RyiSnow on Youtube. The tutorial is good though, at times I throw in my own changes, and will likely rewrite everything at the end to adjust how it is organized.
One of the changes I implemented is a word wrap static function that looks at the width given and determines if the next word in a string will fit in the space, it will then break lines up into an array of strings for the return. I included an edge case where a single word could be longer than the space in one line. "AHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH "
This solution uses an ArrayList to store the return strings. An ArrayList was used so that we can continually add to the list while not knowing how large the array will be.
Next, we use StringBuilder over concatenating string with "text" + "here" which can have a negative effect on performance. This could have a large impact depending on how the method is called. In the game code, this method is called on every frame, and we would see a performance hit because of this.
This performance hit of calling the method each frame can further be mitigated by creating a cache of the return array that is used if the method has already been called.
There are three inputs to the class:
- Graphics2D- This is used to get font data so we can calculate the line lengths
- String- The text in
- int - The size of the area that we have as an int
Overall, it works fairly well, and I don't see a big performance hit ever when running the method each frame (which you shouldn't do).
public static String[] wrapText(Graphics2D graph2D, String textIn, int textWidthArea) {
String[] myReturnString;
int initialLength = (int) graph2D.getFontMetrics().getStringBounds(textIn, graph2D).getWidth();
// If String is smaller than the area we have just return it;
if (initialLength < textWidthArea) {
myReturnString = new String[1];
myReturnString[0] = textIn;
return myReturnString;
}
ArrayList<String> returnLinesArray = new ArrayList<String>();
StringBuilder stbld = new StringBuilder();
int currentLineLength = 0;
// for each of the words in textIn
for (String st : textIn.split(" ")) {
int currentwordlength = (int) graph2D.getFontMetrics().getStringBounds(st + " ", graph2D).getWidth();
// is the current word shorter than the total text area?
if (currentwordlength <= textWidthArea) {
// if it fits add it to the next list
if (currentLineLength + currentwordlength <= textWidthArea) {
stbld.append(st).append(" ");
currentLineLength += currentwordlength;
} else {
returnLinesArray.add(stbld.toString());
stbld = new StringBuilder();
stbld.append(st).append(" ");
currentLineLength = currentwordlength;
}
} else {
//our word is to long, lets break it up into two lines ( this is probably an edge case )
char[] textInChars = st.toCharArray();
// foreach char see if the next character will fit.
for (char ch : textInChars) {
// get Character length
int charLength = graph2D.getFontMetrics().charWidth(ch);
//does the character fit in the next character spot?
if (currentLineLength + charLength <= textWidthArea) {
stbld.append(ch);
currentLineLength += charLength;
} else {
returnLinesArray.add(stbld.toString());
stbld = new StringBuilder();
stbld.append(ch);
currentLineLength = charLength;
}
}
//add space at end of the word
stbld.append(" ");
}
}
returnLinesArray.add(stbld.toString());
myReturnString = new String[returnLinesArray.size()];
int currentLine = 0;
for (String st : returnLinesArray) {
myReturnString[currentLine++] = st;
}
return myReturnString;
}