How do you import a font?
'Airacobra Condensed' font available from Download Free Fonts.
import java.awt.*;
import javax.swing.*;
import java.net.URL;
class LoadFont {
public static void main(String[] args) throws Exception {
// This font is < 35Kb.
URL fontUrl = new URL("http://www.webpagepublicity.com/" +
"free-fonts/a/Airacobra%20Condensed.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
JList fonts = new JList( ge.getAvailableFontFamilyNames() );
JOptionPane.showMessageDialog(null, new JScrollPane(fonts));
}
}
OK, that was fun, but what does this font actually look like?
import java.awt.*;
import javax.swing.*;
import java.net.URL;
class DisplayFont {
public static void main(String[] args) throws Exception {
URL fontUrl = new URL("http://www.webpagepublicity.com/" +
"free-fonts/a/Airacobra%20Condensed.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, fontUrl.openStream());
font = font.deriveFont(Font.PLAIN,20);
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
JLabel l = new JLabel(
"The quick brown fox jumps over the lazy dog. 0123456789");
l.setFont(font);
JOptionPane.showMessageDialog(null, l);
}
}
You can use GraphicsEnvironment.registerFont
http://docs.oracle.com/javase/6/docs/api/java/awt/GraphicsEnvironment.html#registerFont(java.awt.Font)
With this you can load a font from a .ttf file:
private static final Font SERIF_FONT = new Font("serif", Font.PLAIN, 24);
private static Font getFont(String name) {
Font font = null;
if (name == null) {
return SERIF_FONT;
}
try {
// load from a cache map, if exists
if (fonts != null && (font = fonts.get(name)) != null) {
return font;
}
String fName = Params.get().getFontPath() + name;
File fontFile = new File(fName);
font = Font.createFont(Font.TRUETYPE_FONT, fontFile);
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
ge.registerFont(font);
fonts.put(name, font);
} catch (Exception ex) {
log.info(name + " not loaded. Using serif font.");
font = SERIF_FONT;
}
return font;
}