10.5 Using Local Fonts
In Java 2D you can use the same logical font names as in Java 1.1, namely, Serif (e.g., Times), SansSerif (e.g., Helvetica or Arial), Monospaced (e.g., Courier), Dialog, and DialogInput. However, you can also use arbitrary local fonts installed on the platform if you first look up the entire list, which may take a few seconds. Look up the fonts with the getAvailableFontFamilyNames or getAllFonts methods of GraphicsEnvironment. For example:
GraphicsEnvironment env = GrapicsEnvironment.getLocalGraphicsEnvironment();
Then, add
env.getAvailableFontFamilyNames();
or
env.getAllFonts(); // Much slower!
Despite a misleading description in the API, trying to use an available local font without first looking up the fonts, as above, gives the same result as asking for an unavailable font: a default font instead of the actual one.
Core Warning
Trying to use a local font without first looking up the fonts results in a default font being used instead of the actual one. You only need to incur this overhead the first time that you need to create a new Font object.
Note that getAllFonts returns an array of real Font objects that you can use like any other Font but is much slower. If all you need to do is tell Java to make all local fonts available, always use getAvailableFontFamilyNames.
The best approach is to loop down getAvailableFontFamilyNames, checking for the preferred font name and having several backup names to use if the first choice is not available. If you pass an unavailable family name to the Font constructor, a default font (SansSerif) is used. Listing 10.8 provides the basic code for listing all available fonts on the platform.
Listing 10.8 ListFonts.java
import java.awt.*; /** Lists the names of all available fonts. */ public class ListFonts { public static void main(String[] args) { GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] fontNames = env.getAvailableFontFamilyNames(); System.out.println("Available Fonts:"); for(int i=0; i<fontNames.length; i++) System.out.println(" " + fontNames[i]); } }
Listing 10.9 gives a simple example of first looking up the available fonts on the system and then setting the style to Goudy Handtooled BT prior to drawing the String "Java 2D". The result is shown in Figure 105. On platforms without Goudy Handtooled BT, the text is drawn in the default font, SansSerif.
Listing 10.9 FontExample.java
import java.awt.*; /** An example of using local fonts to perform drawing in * Java 2D. */ public class FontExample extends GradientPaintExample { public FontExample() { GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); env.getAvailableFontFamilyNames(); setFont(new Font("Goudy Handtooled BT", Font.PLAIN, 100)); } protected void drawBigString(Graphics2D g2d) { g2d.setPaint(Color.black); g2d.drawString("Java 2D", 25, 215); } public void paintComponent(Graphics g) { clear(g); Graphics2D g2d = (Graphics2D)g; drawGradientCircle(g2d); drawBigString(g2d); } public static void main(String[] args) { WindowUtilities.openInJFrame(new FontExample(), 380, 400); } }
Figure 105 In Java 2D, writing text in any local font installed on the platform is possible.