×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    15

    Show a text with WordWrap

    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.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Location
    London, UK
    Posts
    8
    Nope.
    You have to split the text, the Font.stringWidth should help.

  3. #3
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    15
    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?

  5. #5
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395
    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!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×