How to show a text with WordWrap? Is there a predefined class? I would like to show a very long string on screen (up to 200 chars) and the Graphics.drawString(...) method draws a line and most of the string is lost outside the screen.
How to show a text with WordWrap? Is there a predefined class? I would like to show a very long string on screen (up to 200 chars) and the Graphics.drawString(...) method draws a line and most of the string is lost outside the screen.
Nope.
You have to split the text, the Font.stringWidth should help.
Big Thanks for the Link! But if i want to show a text over several pages like the about-box-dialog on the 7650, what is the best way to handly this?
You could use an Alert... they will word-wrap, and will scroll if you have more text than will fit on the screen.
Otherwise, it's the hard way, I'm afraid.
Store your text in a char[]. Create an int[] - you'll use this to store the index of the first character for each line (or use a Vector if you have no idea how many lines you are likely to split your text into).
The first line starts at index zero.
Iterate through the char[] looking for spaces - you only need to know where the most recently encountered space is. As you go, add up the widths of the characters using Font.charWidth().
When the total width exceeds the width of the display area, jump back to the last space. This is the last character of the line, so the character that follows it is the first character of the next line - record this index in your int[] (or Vector), then repeat the process from this point.
At the end of the char[] you will know how many lines you have, and where each begins. By subtracting the start of each line from the start of the next, you can work out the number of characters in the line, and so draw it with:
g.drawChars (achMyText, nStart, nLength, x, y, anchor);
You can work out how many lines will fit on the screen by dividing the display area height by the height of the font.
Then your paint() implementation can draw text for lines from (currentTopLine) to (currentTopLine + (linesPerPage - 1)). Code in your keyPressed() event can increment or decrement currentTopLine (or add or subtract a page-length if you prefer).
Have fun!!