gpt4 book ai didi

java - 我应该如何修复这个字体加载器?

转载 作者:行者123 更新时间:2023-12-01 11:47:44 26 4
gpt4 key购买 nike

所以我试图创建一个返回字体对象的方法 - 从 .ttf 派生它,但我的代码不起作用:

public Font loadFont(){
Font font = null;
try {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
font = Font.createFont(Font.TRUETYPE_FONT, new File("vgafix.ttf"));
} catch (Exception e) {
//Handle exception
}
return font;
}

我该如何解决这个问题?

最佳答案

it's just not drawing anything. If, instead of setting the font to null, i set it to a java default font, it works

创建时,默认的字体大小为1。尝试使用类似的东西

Font font = loadFont().deriveFont(12f);

设置所需的字体大小

the file is in the same dir as the rest of the classfiles

您可能还会发现,当字体驻留在应用程序上下文中时,使用File 引用该字体可能意味着找不到该文件。例如,加载嵌入式资源时,您应该使用类似于 Class#getResourceClass#getResourceAsStream 的内容,具体取决于您的需要

font = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("/package/path/to/resource/vgafix.ttf"));

关于java - 我应该如何修复这个字体加载器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29025125/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com