13

I have a custom font I want to show off in a Java program where the user can view it without having to install it. Does anyone know how to do that?

working solution


I have implemented the following:

font = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, new java.io.File(Clazz.class.getResource("/resources/segoescb.ttf").toURI()));
font = font.deriveFont(11.0F);

1 Answer 1

12

I have never done it, but it seems like the methods you want are

Importing the Font

Font createFont(int fontFormat, InputStream fontStream)

or alternatively

Font createFont(int fontFormat, File fontFile)

The int parameter is either Font.TRUETYPE_FONT or Font.TYPE1_FONT, while the InputStream or File parameter contains the font's binary data.

Using the Font after import:

To make the Font available to Font constructors the returned Font must be registered in the GraphicsEnviroment by calling registerFont(Font).

5
  • Okay,I got that to work... except it makes the font size 1. How can I actually make it usable?
    – Ky -
    Commented Dec 20, 2010 at 9:38
  • 1
    My guess is with this method: download.oracle.com/javase/6/docs/api/java/awt/… Commented Dec 20, 2010 at 9:44
  • If you would allow me to revisit this, it doesn't seem to work with a jar file. Do you have a solution to this?
    – Ky -
    Commented Dec 21, 2010 at 17:57
  • @Supuhstar what's in the jar file? The font? Or the app? Or both? Commented Dec 21, 2010 at 18:32
  • 2
    For a JAR file, use the InputStream version of createFont instead of the File version. Commented May 25, 2011 at 22:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.