Drawing exotic fonts in a java applet

风格不统一 提交于 2019-12-24 07:11:39

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/8692095/drawing-exotic-fonts-in-a-java-applet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!