开发者

How can I use a custom font in Java?

I wrote a program in Java that uses a special font that by default doesn't exist on any operating system.

Is it possible in Java to add this开发者_如何学C special font to the operation system? For example, in Windows, to copy this font to the special Fonts folder.

If it is possible, how?


If you include a font file (otf, ttf, etc.) in your package, you can use the font in your application via the method described here:

Oracle Java SE 6: java.awt.Font

There is a tutorial available from Oracle that shows this example:

try {
     GraphicsEnvironment ge = 
         GraphicsEnvironment.getLocalGraphicsEnvironment();
     ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("A.ttf")));
} catch (IOException|FontFormatException e) {
     //Handle exception
}

I would probably wrap this up in some sort of resource loader though as to not reload the file from the package every time you want to use it.

An answer more closely related to your original question would be to install the font as part of your application's installation process. That process will depend on the installation method you choose. If it's not a desktop app you'll have to look into the links provided.


Here is how I did it!

//create the font

try {
    //create the font to use. Specify the size!
    Font customFont = Font.createFont(Font.TRUETYPE_FONT, new File("Fonts\\custom_font.ttf")).deriveFont(12f);
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    //register the font
    ge.registerFont(customFont);
} catch (IOException e) {
    e.printStackTrace();
} catch(FontFormatException e) {
    e.printStackTrace();
}

//use the font
yourSwingComponent.setFont(customFont);


If you want to use the font to draw with graphics2d or similar, this works:

InputStream stream = ClassLoader.getSystemClassLoader().getResourceAsStream("roboto-bold.ttf")
Font font = Font.createFont(Font.TRUETYPE_FONT, stream).deriveFont(48f)


From the Java tutorial, you need to create a new font and register it in the graphics environment:

GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("A.ttf")));

After this step is done, the font is available in calls to getAvailableFontFamilyNames() and can be used in font constructors.


Configuration:

final GraphicsEnvironment GE = GraphicsEnvironment.getLocalGraphicsEnvironment();
final List<String> AVAILABLE_FONT_FAMILY_NAMES = Arrays.asList(GE.getAvailableFontFamilyNames());
try {
    final List<File> LIST = Arrays.asList(
        new File("font/JetBrainsMono/JetBrainsMono-Thin.ttf"),
        new File("font/JetBrainsMono/JetBrainsMono-Light.ttf"),
        new File("font/Roboto/Roboto-Light.ttf"),
        new File("font/Roboto/Roboto-Regular.ttf"),
        new File("font/Roboto/Roboto-Medium.ttf")
     );
     for (File LIST_ITEM : LIST) {
         if (LIST_ITEM.exists()) {
             Font FONT = Font.createFont(Font.TRUETYPE_FONT, LIST_ITEM);
             if (!AVAILABLE_FONT_FAMILY_NAMES.contains(FONT.getFontName())){ 
                 GE.registerFont(FONT);
             }
         }
     }
} catch (FontFormatException | IOException exception) {
    JOptionPane.showMessageDialog(null, exception.getMessage());
}

Usage example:

JLabel label1 = new JLabel("TEXT1");
label1.setFont(new Font("Roboto Medium", Font.PLAIN, 13));

JLabel label2 = new JLabel("TEXT2");
label2.setFont(new Font("Roboto", Font.PLAIN, 13));

Note: Only existing fonts that are not available in the system are registered.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜