Java 2D™ defines the following five logical font families:
Dialog
DialogInput
Monospaced
Serif
SansSerif
These fonts are available on any Java platform and can be thought of as aliases for some underlying font that has the properties implied by its name. A Serif font is a font similar to Times New Roman, which is commonly used in print. A Sans Serif font is more typical for onscreen use.
These fonts can be customized for the locale of the user. In addition these fonts support the widest range of code points (unicode characters).
Apart from the family, fonts have other attributes, the most important of which are style and size. Styles are Bold and Italic.
The default Java 2D font is 12 pt Dialog. This font is a typical point size for reading text on a normal 72–120 DPI display device. An application can create an instance of this font directly by specifying the following:
Font font = new Font("Dialog", Font.PLAIN, 12);
In addition to the logical fonts, Java software provides access to other fonts that are installed on your system. The names of all available font families can be found by calling the following:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String []fontFamilies = ge.getAvailableFontFamilyNames();
The FontSelector sample program (available in
FontSelector.java
)
illustrates how to locate and select these
fonts. You can use this example to view how Sans Serif appears on your system.
These other fonts are called physical fonts.
Sometimes, an application cannot depend on a font being installed on the system,
usually because the font is a custom font that is not otherwise available.
In this case, the application must include the font.
This lesson shows how to obtain a TrueType
font, the most commonly
used font type on modern operating systems, to a java.awt.Font
object.
You can use either of these methods:
Font java.awt.Font.createFont(int fontFormat, InputStream in); Font java.awt.Font.createFont(int fontFormat, File fontFile);
fontFormat
must be the constant Font.TRUETYPE_FONT
.
Font font = Font.createFont(Font.TRUETYPE_FONT, new File("A.ttf"));
Accessing the font directly from a file must be more convenient for some cases.
However, an InputStream
might be needed if your code is unable to access file
system resources, or if the font is packaged in a Java Archive (JAR) file along with
the rest of the application or applet.
The returned Font
instance can then be used with the Font.deriveFont(..)
methods to derive a version that is the required size. For example:
try { /* Returned font is of pt size 1 */ Font font = Font.createFont(Font.TRUETYPE_FONT, new File("A.ttf")); /* derive and return a 12 pt version : need to use float otherwise * it would be interpreted as style */ return font.deriveFont(12f); } catch (IOException ioe); } catch (FontFormatException ffe); }
It is important to use deriveFont()
because fonts which are created by application
are not part of the set of fonts known to the underlying font system.
Because deriveFont
works from the original created font it does not have
this limitation.
The solution for this problem is to register the created font with the graphics environment. For example:
try { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("A.ttf")); } catch (IOException ioe); } catch (FontFormatException ffe); }
getAvailableFontFamilyNames()
and can be used in font constructors.