How to Use a Custom Font in Java

How can I use a custom font in Java?

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.

Using a custom font for a JLabel

@sasankad is mostly correct (+1).

Once you have created the font, it will have a default size of 1

Font font = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("/CUSTOMFONT-MEDIUM.TTF"));  

You then need to derive the font size and style you want.

Font biggerFont = font.deriveFont(Font.BOLD, 48f);

Sample Image

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestCustomFont {

public static void main(String[] args) {
new TestCustomFont();
}

public TestCustomFont() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class TestPane extends JPanel {

public TestPane() {
setLayout(new GridBagLayout());
try {
Font font = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("/Royal Chicken.ttf"));
JLabel happy = new JLabel("Happy little Miss Chicken");
happy.setFont(font.deriveFont(Font.BOLD, 48f));
add(happy);
} catch (FontFormatException | IOException ex) {
ex.printStackTrace();
}
}

}

}

Check out java.awt.Font for more details...

You may also want to take a look at Physical and Logical Fonts, Font Configuration Files

Java Load Custom Font File (.ttf)

It isn't that difficult to load a font from resources the same way you are loading the images. I know this question was asked a year ago, but i hope to finally provide an answer.

Simply use the documentation here, which details how to load custom fonts into a GraphicsEnvironment. It should look something like the following:

    GraphicsEnvironment ge = null;
try{
ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, game.getClass().getResourceAsStream("/fonts/fantasy.TTF")));
} catch(FontFormatException e){} catch (IOException e){}

Note: I use classInstance.getClass().getResourceAsStream(String fileDir) to load the file from my resources directory in the Jar file.

After registering the font with the graphics environment, the font is available in calls to getAvailableFontFamilyNames() and can be used in font constructors.

Hope this answers your question finally!

Java - How to Load a Custom Font From a Resources Folder

You may want to have a read on Accessing Resources in Java.

You need leading / denotes the root of your class path as well as the resource package name.

Nunito = CustomFont("/resources/Fonts/Nunito/Nunito-BlackItalic.ttf");

With that being said I can't tell where you class is, it's weird your resources folder/package is separate from your java usually they would be in the same folder just in a different package.

Can i use custom font for the image in java?

i get the custom font through this code

Font font = Font.createFont(Font.TRUETYPE_FONT, new File("C:\\Pacifico.ttf"))
.deriveFont(48 f);

Can't set custom font

As from JavaFX version 1.8u60 you can use plain css to load the font but with carefull that you need to use the exactly original name of the Font File,for example:

@font-face{
src: url("../fonts/Younger than me Bold.ttf");
}

.button{
-fx-background-color:white;
-fx-background-radius:15.0;
-fx-font-size:18.0;
-fx-font-family:"Younger than me";
-fx-text-fill:black;
-fx-font-weight:bold;
}

Mention that the original name can be found opening the .ttf with a default editor and see it's name in case if you don't know it.

Complete tutorial:http://www.guigarage.com/2014/10/integrate-custom-fonts-javafx-application-using-css/

Have a look also here for your situation:Specifying external font in JavaFX CSS


Finally:

The problem in your code is that in the css you are not adding double quotes " or single quotes ' around the font - family

Custom Fonts in Java

Here's an utility method I'm using to load a font file from a .ttf file (can be bundled):

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;
}


Related Topics



Leave a reply



Submit