Font question

I am using a font in my program which may not be available to my users. I need to distribute the ttf file for the font along with my application JAR. Can anyone please advice where I need to place the ttf file within my application directory in order to be able to load it from within my program and also distribute it as part of my JAR. (I am using NetBeans 5.5)

Thanks a lot in advance!

Message was edited by:

pradeep@ech

[449 byte] By [pradeep@echa] at [2007-11-27 5:06:36]
# 1
Use the static method Font.createFont(int, File) or Font.createFont(int, InputStream) to get a Font object representing the font you want. You can put the font file wherever you like, provided you can construct a File object or an InputStream for that file. Greets, Mike
MikePa at 2007-7-12 10:25:23 > top of Java-index,Desktop,Core GUI APIs...
# 2

Erm, in case your question was more like "How do I load a data file from a JAR?" instead of "How do I create a font?", here's the standard approach:

Alternative 1:

URL url = getClass().getResource("fonts/MyFont.ttf");

File f = new File(url.toURI());

Alternative 2:

InputStream is = getClass().getResourceAsStream("fonts/MyFont.ttf");

In each case, the String parameter is the path to the resource relative to the package directory of the calling class, i.e. if you call this in class my.package.Foo, then the path to MyFont.ttf within the JAR must be "my/package/fonts/MyFont.ttf". If you rather want to put the font under "fonts/MyFont.ttf" in the top-level JAR, then use the same methods above on the ClassLoader instead of the Class object.

MikePa at 2007-7-12 10:25:23 > top of Java-index,Desktop,Core GUI APIs...
# 3

Thanks for the reply....

In my project folder ABC, i have a src folder in which i have created a package named 'display'...and in it i have kept the ttf file..

In the code i wrote the same as what you suggested....

URL url = getClass().getResource("ARIALUNI.ttf");

File f = new File(url.toURI());

font=Font.createFont(Font.TRUETYPE_FONT,f);

font = font.deriveFont(Font.BOLD,22f);

But when i transfer the JAR file created in the 'dist' folder...its not recognizing the font...though the font is packed with the jar....

any idea on what is happening....

pradeep@echa at 2007-7-12 10:25:23 > top of Java-index,Desktop,Core GUI APIs...
# 4
Are you sure the ttf file is being copied into the JAR file by NetBeans?Posting a Short Self-Contained Compilable Example ( http://homepage1.nifty.com/algafield/sscce.html) can help.
MikePa at 2007-7-12 10:25:23 > top of Java-index,Desktop,Core GUI APIs...
# 5

I am sure that the JAR contains the font. It is the Arial Unicode MS font, which is quite big (22 mb). There is no mistaking that the JAR contains the font file.

The code works fine when I run it from within the IDE, but the font does not seem to be loaded when I distribute the JAR.

Any idea what else I might be doing wrong.

pradeep@echa at 2007-7-12 10:25:23 > top of Java-index,Desktop,Core GUI APIs...
# 6

> The code works fine when I run it from within the

> IDE, but the font does not seem to be loaded when I

> distribute the JAR.

> Any idea what else I might be doing wrong.

From what you're saying, I can't tell what's wrong. I just tested the above solution and it works for me when packed within a JAR.

You would be best helped if you would upload the whole JAR somewhere and post a link to it so that we can download and test it (preferably not with a 22 MB font :) ) The JAR should contain the source code of a short sample program demonstrating the problem.

If for some reason you cannot manage that, try the following:

1. Using the ClassLoader instead of the Class object to load resources.

2. Using the getResourceAsStream method instead of the URL.

3. Loading some other resource instead of a font file, for example an icon or a sound file.

Hope this helps,

Mike

MikePa at 2007-7-12 10:25:23 > top of Java-index,Desktop,Core GUI APIs...
# 7
Thanks a lot!That takes care of my problem. I used the getResourceAsStream method and everything is fine now.
pradeep@echa at 2007-7-12 10:25:23 > top of Java-index,Desktop,Core GUI APIs...