/* * [TestCreateFont.java] * * Summary: demonstrate use of java.awt.Font.createFont. * * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2009-01-01 initial version */ package com.mindprod.example; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; import java.awt.Color; import java.awt.Font; import java.awt.FontFormatException; import java.io.IOException; import java.io.InputStream; import static java.lang.System.*; /** * demonstrate use of java.awt.Font.createFont. *

* How to include a custom font with your application. *

* Accessing an uninstalled Font bundled in a jar or on the classpath. *

* Note this technique is illegal if you don't have licensing permission from the font designer. This only works in * Swing! * * @author Roedy Green, Canadian Mind Products * @version 1.0 2009-01-01 initial version * @since 2009-01-01 */ public final class TestCreateFont { /** * Debugging harness for a Frame * * @param args command line arguments are ignored. */ public static void main( String args[] ) { SwingUtilities.invokeLater( new Runnable() { public void run() { final JFrame frame = new JFrame(); // Uninstalled font is a resource on classpath or in jar named // "com/mindprod/example/IGLOOLAS.TTF" // This example font is a decorative font with snowcaps // on the capital letters. try { InputStream fontStream = TestCreateFont.class .getResourceAsStream( "IGLOOLAS.TTF" ); Font onePoint = Font.createFont( Font.TRUETYPE_FONT, fontStream ); fontStream.close(); Font snowFont = onePoint.deriveFont( Font.PLAIN, 18 ); out.println( snowFont ); JLabel label = new JLabel( "SNOW ALERT" ); label.setBackground( Color.WHITE ); label.setForeground( Color.BLACK ); label.setFont( snowFont ); frame.getContentPane().add( label ); frame.setSize( 300, 100 ); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); frame.validate(); frame.setVisible( true ); } catch ( FontFormatException e ) { err.println( "FontFormaException: " + e.getMessage() ); System.exit( 1 ); } catch ( IOException e ) { err.println( "IOException: " + e.getMessage() ); System.exit( 1 ); } } } ); } // end main } // end class