みなさん、こんにちは。カスタム フォントを Java にロードする際に問題が発生しています。
public class CustomFonts extends JPanel {
public static void loadFont() throws FontFormatException, IOException {
String fontFileName = "stocky.ttf";
InputStream is = CustomFonts.class.getClassLoader()
.getResourceAsStream(fontFileName);
Font ttfBase = Font.createFont(Font.TRUETYPE_FONT, is);
Font ttfReal = ttfBase.deriveFont(Font.PLAIN, 24);
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();
ge.registerFont(ttfReal);
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setTitle("Blach Blach Blach");
frame.setSize(400, 400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
try {
loadFont();
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
JLabel fontF = new JLabel("Testing 1, 2, 3");
fontF.setFont(new Font("ttfReal", Font.PLAIN, 20));
frame.add(fontF);
}
}
コードを実行すると、フォントがデフォルトのように見えます。ttf ファイルを Eclipse のプロジェクト フォルダーにロードしましたが、ファイルへの明示的なルートを指定する必要がありますか? より大きなプログラムにフォントをロードしようとしているため、この基本的なプログラムを通じてフォントを理解しようとしています。