Recipe 13.7 Drawing Text with an Application Font


You want to provide a font with your application but not require users to install it as a "system font" on all platforms.


Use Font.createFont(...) , which returns a scalable Font object, and scale it with deriveFont(int nPoints).


JDK 1.3 introduced the static method Font.createFont( ), which allows you to have a "private" font that can be used in your application without having it installed using the operating system's font mechanism. Users can then use your application and its custom font without having to have "root" or "administration" privileges on systems that require this in order to install fonts.

The createFont( ) method requires two arguments. The first is an int, which must be the public static field Font.TRUETYPE_FONT , and the second is an InputStream (see Recipe 10.15) that is open for reading the binary file. As you can infer from the requirement that the first argument be Font.TRUETYPE_FONT, only TrueType fonts are supported at present. The Font class documentation, ever the optimist, states that this field is to allow possible future addition of other font formats, though none is promised. Given the availability of free PostScript font renderers, such as the one in the X Window System XFree86, it should be possible to add PostScript font support in the future. Example 13-3 is a listing of a small standalone application that creates the window shown in Figure 13-5.

Figure 13-5. TTFontDemo in action

Example 13-3. Demo of an application font
/** Demo of making TrueType font usable in Java. This is a way cool facility  * because it means you can have "application-specific" fonts in Java;  * your application can have its own distinctive font that the user does  * NOT have to install into the JRE before you can use it.  * (of course they can install it if they have privileges and want to).  * <p>  * Must remain Swing-based despite problems on older systems, since  * apparently only Swing components can use TTF fonts in this implementation.  * <p>  * Did  NOT work for me in Applet  nor  JApplet due to  * security problems (requires to create a temp file). Could be made  * to work by providing a policy file.  * @author    Ian Darwin  * @since 1.3  */ public class TTFontDemo extends JLabel {     /** Construct a TTFontDemo -- Create a Font from TTF.      */     public TTFontDemo(String fontFileName, String text)     throws IOException, FontFormatException {         super(text, JLabel.CENTER);         setBackground(Color.white);         // First, see if we can load the font file.         InputStream is = this.getClass( ).getResourceAsStream(fontFileName);         if (is == null) {             throw new IOException("Cannot open " + fontFileName);         }         // createFont makes a 1-point font, bit hard to read :-)         Font ttfBase = Font.createFont(Font.TRUETYPE_FONT, is);         // So scale it to 24 pt.         Font ttfReal = ttfBase.deriveFont(Font.PLAIN, 24);         setFont(ttfReal);     }     /** Simple main program for TTFontDemo */     public static void main(String[] args) throws Exception {         String DEFAULT_MESSAGE =              "What hath man wrought? Or at least rendered?";         String DEFAULT_FONTFILE =             "Kellyag_.ttf";         String message = args.length == 1 ? args[0] : DEFAULT_MESSAGE;         JFrame f = new JFrame("TrueType Font Demo");         TTFontDemo ttfd = new TTFontDemo(DEFAULT_FONTFILE, message);         f.getContentPane( ).add(ttfd);         f.setBounds(100, 100, 700, 250);         f.setVisible(true);         f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);     } }

This font technology has some restrictions. First, as noted in the comments, you can use this font only on a Swing JComponent, not on an AWT Component (see Chapter 14).

Also, this technique cannot easily be used in an applet. The createFont( ) method obviously requires some very clever code and, for this reason, is apparently unable to do its work from an opened InputStream; it copies the TrueType font file to the local disk to work on it. This may cause a security exception to be thrown. The program in TTFontApplet in the online source shows an applet that attempts to reuse the code from the TTFontDemo program. Invoking this as an applet fails when used with Netscape 4 due to lack of a complete Swing implementation; it throws a SecurityException when used with the Java Plug-in under older Netscape versions, but works fine when invoked under Apple's browser, Safari, which uses the latest installed JRE (in my case, Java 1.4.1_03). And it should also work in an application downloaded using Java WebStart (see Recipe 23.13).

Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: