Drawing exotic fonts in a java applet

2019-08-06 03:26发布

I'd like to draw symbols contained in exotic fonts such as Wingdings, Webdings (they are called dingbats : http://en.wikipedia.org/wiki/Dingbat) in a java applet. When using Graphics.drawString() I end up with the typical 'square symbol' for each character in the drawn string. I could not find any answer by googling and I guess it's not something common.

In the past I've done it with C# WPF, failed to do it with SVG and HTML. Is it possible in java?

EDIT: Solved. There was a subtlety. Even though the symbol you want in such a font is linked to character 'a', it is in fact necessary to pass "\uF061" to g.drawString() and not "a". Otherwise you end up with the square symbol.

2条回答
Viruses.
2楼-- · 2019-08-06 04:03

Add the Font to a Jar (assuming you have the right to distribute it) that is on the run-time class-path of the applet. I.E. Add a reference to the Jar to the archive attribute of the applet element. Access it via (something like):

URL dingbatURL = this.getClass().getResource("/fonts/dingbat.ttf");

Then look to the Font methods for the createFont(int,InputStream) method.

Font dingbatFont = Font.createFont(Font.PLAIN,dingbatURL.openStream());

Thereafter is should be possible to use the Font in the Graphics instance.

查看更多
倾城 Initia
3楼-- · 2019-08-06 04:17

Solved. Even though the symbol you want in such a font is linked to character 'a', it is in fact necessary to pass "\uF061" to g.drawString() and not "a". Otherwise you end up with the square symbol.

查看更多
登录 后发表回答