I'm trying to make a textbox for a small game and I've run into an issue. I'm using drawString, which doesn't support \n so I've made an array of strings for multiple lines like so. Also, there is a global int called lettersShown that will be important later.
int lettersShown = 0;
String[] result = new String[] {" "," "," "," "," "," "," "," "};
In the constructor of the class, I then created a random event containing an array of 8 strings(Sorry if this is long and poorly formatted, its just for testing purposes.)
travel.add(new Event(this, new String[]{"You walk along a winding dirt
road,", "exposed to the scorching of the sun.",
"Every so often, you pass the remains", "of blasted homes
and dilapidated", "hovels. The air is especially arid",
"today, and by the time the sun drifts", "down into the horizon
you find", "yourself parched."}, -20,0,0,0));
So after I did that, I created a method to draw each line in order.
public void drawText( int x, int y, Graphics g) {
for(int i = 0; i <result.length; i++) {
g.drawString(result[i].substring(0, lettersShown), x, y);
if(result[i].length() > lettersShown) {
lettersShown++;
}
//lettersShown = 0;
y+=15;
}
}
This method works perfectly for any arrays that only have one string, such as this one:
travel.add(new Event(this, new String[] {"You traveled to a new city"},
0,0,0,0));
However, the second it starts to render an event with multiple lines, I get this exception
Exception in thread "Thread-1"
java.lang.StringIndexOutOfBoundsException: String index out of range:23
at java.lang.String.substring(String.java:1963)
at mains.EventManager.drawText(EventManager.java:149)
at mains.Game.render(Game.java:185)
at mains.Game.run(Game.java:87)
at java.lang.Thread.run(Thread.java:748)
I've been trying so many things to fix this seemingly small issue and I'd rather not resort to just having a ton of strings and drawing each of them one at a time so any guidance or anyone who can identify the problem would be greatly appreciated. Thanks!
lettersShown = 4
? \$\endgroup\$