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:
现在,我一直在阅读有关如何使用TextLayout的内容,似乎可以自己编写所有内容,但我更愿意在更高层次上思考。我只想要一个带有这样签名的Label类:
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?
有没有人知道一个开源(非gpl)库,使文本格式简单?
3 个解决方案
#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.
如果您使用java2d绘制游戏图形,则应该能够使用awt或swing文本组件来渲染文本。你可以,例如创建一个JLabel并使用Graphics2D上下文在渲染队列中手动调用其paint和update方法。
JLabel label = new JLabel("your text");
label.setLocation(0, 100);
label.setSize(20, 100);
label.paint(g); // g is your Graphics2D context
#2
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 )
查看这个官方教程:http://download.oracle.com/javase/tutorial/2d/text/index.html它包含了显示文本所需的一切。或者更简单地使用TextLayout,如果你不喜欢阅读=)(http://download.oracle.com/javase/1.4.2/docs/api/java/awt/font/TextLayout.html)
#3
After experimenting with @Daniel's answer, I arrived to the following solution:
在试验@ Daniel的答案后,我得出了以下解决方案:
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...
希望能帮助到你...
#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.
如果您使用java2d绘制游戏图形,则应该能够使用awt或swing文本组件来渲染文本。你可以,例如创建一个JLabel并使用Graphics2D上下文在渲染队列中手动调用其paint和update方法。
JLabel label = new JLabel("your text");
label.setLocation(0, 100);
label.setSize(20, 100);
label.paint(g); // g is your Graphics2D context
#2
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 )
查看这个官方教程:http://download.oracle.com/javase/tutorial/2d/text/index.html它包含了显示文本所需的一切。或者更简单地使用TextLayout,如果你不喜欢阅读=)(http://download.oracle.com/javase/1.4.2/docs/api/java/awt/font/TextLayout.html)
#3
After experimenting with @Daniel's answer, I arrived to the following solution:
在试验@ Daniel的答案后,我得出了以下解决方案:
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...
希望能帮助到你...