I've got 2 JPanel
objects inside a JFrame
and I want to show text in 1 panel and a picture in the other panel. But when the text is too long it disappears behind the image, I don't want that to happen, but I also don't want the word to get split up, so if the word is too long for the remaining space it should go to the next line.
Btw I'm using paintcomponent to draw the text, i use g.drawstring.
My code looks like this :
StringBuilder sb = new StringBuilder($question.getQuestion());
/* After every 30 characters place a \n character, here is where the string will go to the nextline and write the rest of the question there */
int x = 0;
while ((x = sb.indexOf(" ", x + 30)) != -1) {
sb.replace(x, x + 1, "\n");
}
/* Reset x for drawing purposes with a decent offset */
x = 0;
/* Split the string on \n now */
for (String line : sb.toString().split("\n")){
g.drawString(line, 20, 120 + (x*75)); /* Draw the part of the question, x*75 is used for spacing between the substrings of the question */
x++; /* Increment x for drawing purposes with a decent offset */
}
But this still isn't working, if it's a really long word it doesn't cut the word in pieces. EDIT : i found that the problem is that i am looking for a ' ', but if that space doesn't occur any time soon it will keep writing the characters even behind another jpanel. How can i solve this?
Any ideas?