This discussion is archived
4 Replies Latest reply: Apr 29, 2011 7:05 AM by 857860 RSS

Java 2D Box Vertical Text

857860 Newbie
Currently Being Moderated
I'd like to know how to use Java2D to box the vertical text.
For example:

------
| T |
| E |
| S |
| T |
------

Thank you.
  • 1. Re: Java 2D Box Vertical Text
    StanislavL Pro
    Currently Being Moderated
    You can use swing componnet like this
    http://java-sl.com/vertical.html

    Or create AffineTransform and rotate text as desired. Then use normal Graphics drawString() method.

    If you want vertical text but letters should not be rotated just split the row into multiple ones and use multiple drawString() calls for each letter separately
  • 2. Re: Java 2D Box Vertical Text
    857860 Newbie
    Currently Being Moderated
    Dear Stanislav Lapitsky:

    Thank you for your reply.

    I'd like to clarify my question a little bit.

    I'd like to draw vertical text like and box it (that means draw a rectangle around the text). The letter should not be rotated.
    For example:

    T
    E
    S
    T

    and there is a rectangle that wrap around the above text.

    How can I do it using Java 2D? Do I need to use TextLayout?
    Thank you again.

    Edited by: user8407360 on Apr 28, 2011 3:26 PM

    Edited by: user8407360 on Apr 28, 2011 3:27 PM

    Edited by: user8407360 on Apr 28, 2011 3:27 PM
  • 3. Re: Java 2D Box Vertical Text
    StanislavL Pro
    Currently Being Moderated
    You can use TextLayout or don't use it. It's up to you.
    Suppose you would like to start drawin from point x, y. You ot first letter (by substring) and draw it. The you have to add height of the letter to the y position and draw next letter.
    use
            Graphics g;
            g.getFontMetrics().getHeight(); //to get height
            g.getFontMetrics().stringWidth("A"); //OR below
            g.getFontMetrics().charWidth('A');
    Sum of heights is your rect height. Max of letters' widths is your rect width.
  • 4. Re: Java 2D Box Vertical Text
    857860 Newbie
    Currently Being Moderated
    Thank you very much.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points