Using of own fonts are often a problem for your application deployment. A font is a external resource, need to be on running system. You have some alternate ways: You deploy your font with your application (do not work with applets), you hope the font is installed or your deploy your font within your application.

This Article only describe the last way - deploy font within your application.

What we have to do:

  1. Deploy the font in our application for example in the java archive.
  2. At running time load our font.
  3. Use our font.

Font deploy

Copy your font in one directory from java archive .

Load font

We load our font with java.awt.Font.createFont() method. Because the size of font is 1 point - not enought and so we use java.awt.Font.deriveFont(float) to resize it.

  InputStream fin = this.getClass().getResourceAsStream("Asimov.ttf");
this.asimov = Font.createFont ( // Load font from InputStream fin
Font.PLAIN,
fin
).deriveFont(24f);

 

Use font

At the end we need to use the font. In our simple sample we use the java.awt.Component.setFont() method.

  second.setFont(this.asimov); // Use our font