6

This is what I need in my game regarding text:

Word Wrap Support given a bounding box
Vertical and Horizontal alignment given a bounding box

Now, I've been reading about how to use TextLayout, and it seems possible to write all this myself but I'd prefer to think at a higher level. I just want a Label class with a signature like this:

public Label(String text, Alignment alignment, VAlignment vAlignment);

Does anyone know of an open source (non-gpl) library out there that makes text formatting simple?

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356

3 Answers3

1

If you are using java2d to paint your game graphics, you should be able to use the the awt or swing text components to render your text. You could e.g. create a JLabel and call its paint und update methods manually in your render queue with your Graphics2D context.

JLabel label = new JLabel("your text");
label.setLocation(0, 100); 
label.setSize(20, 100);
label.paint(g); // g is your Graphics2D context
Daniel Seidewitz
  • 720
  • 8
  • 23
1

Check this official tutorial: http://download.oracle.com/javase/tutorial/2d/text/index.html It contains everything you'll ever need for displaying text. Or more simply use TextLayout, if you don't like reading=) ( http://download.oracle.com/javase/1.4.2/docs/api/java/awt/font/TextLayout.html )

jusio
  • 9,850
  • 1
  • 42
  • 57
0

After experimenting with @Daniel's answer, I arrived to the following solution:

    JLabel label = new JLabel("your text");
    label.setSize(screen); //screen is a Dimension Object..
    label.setHorizontalAlignment(SwingConstants.CENTER);
    label.paint(g2d); // g is your Graphics2D context

Hope it helps...

Mazyod
  • 22,319
  • 10
  • 92
  • 157