问题描述
我已经使用了下面的代码,它出现了这个堆栈跟踪:
pre $ java.io.FileNotFoundException:font .ttf(没有这样的文件或目录)
在java.io.FileInputStream.open(Native方法)$ b $在java.io.FileInputStream。< init>(FileInputStream.java:146)
在java.io.FileInputStream。< init>(FileInputStream.java:101)
at com.ominious.core.graphics.Assets.getFont(Assets.java:55)
at com.ominious .core.graphics.Assets.loadImages(Assets.java:37)
at com.ominious.core.GamePanel.init(GamePanel.java:63)
at com.ominious.core.GamePanel.run (GamePanel.java:69)
在java.lang.Thread.run(Thread.java:744)
线程Thread-1中的异常java.lang.NullPointerException $ b $ com at com。 ominious.core.graphics.Assets.loadImages(Assets.java:49)
at com.ominious.core.GamePanel.init(GamePanel.java:63)
at com.ominious.core.GamePanel。运行(GamePanel.java:69)
在java.lang.Thread.run(Thread.j ava:744)
我使用这段代码(我在资源文件中调用方法, )
private static Font getFont(String name)throws Exception {
Font font = Font.createFont (Font.TRUETYPE_FONT,new FileInputStream(name));
返回字体;
$ / code>
我在这里调用它:
try {
FONT = getFont(font.ttf);
tileSprites = ImageIO.read(getClass()。getResourceAsStream(/ mom.gif));
SPLASH_BACKGROUND = ImageIO.read(getClass()。getResourceAsStream(/ swag.gif));
} catch(Exception e){
Game.logger.log(LogType.ERROR_STACKTRACE);
e.printStackTrace();
(上面的类可以加载我的图片)
有没有一个原因,这是行不通的?有更好的方法吗? (是的,我已经在我的目录中)
解决方案很可能是因为你正在运行这个jar,并且没有 File
对象来获取。比较你如何加载你的图像与 getResourceAsStream
,它可以找到解压为文件(通常用于开发)或打包到jar中的资源。在 createFont
中使用相同的 getResourceAsStream
。
I have used this code below, and it comes up with this stack trace:
java.io.FileNotFoundException: font.ttf (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at com.ominious.core.graphics.Assets.getFont(Assets.java:55)
at com.ominious.core.graphics.Assets.loadImages(Assets.java:37)
at com.ominious.core.GamePanel.init(GamePanel.java:63)
at com.ominious.core.GamePanel.run(GamePanel.java:69)
at java.lang.Thread.run(Thread.java:744)
Exception in thread "Thread-1" java.lang.NullPointerException
at com.ominious.core.graphics.Assets.loadImages(Assets.java:49)
at com.ominious.core.GamePanel.init(GamePanel.java:63)
at com.ominious.core.GamePanel.run(GamePanel.java:69)
at java.lang.Thread.run(Thread.java:744)
I use this code (I call the method in a resource file that I know works)
private static Font getFont(String name) throws Exception {
Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(name));
return font;
}
And I call it here:
try {
FONT = getFont("font.ttf");
tileSprites = ImageIO.read(getClass().getResourceAsStream("/mom.gif"));
SPLASH_BACKGROUND = ImageIO.read(getClass().getResourceAsStream("/swag.gif"));
} catch (Exception e) {
Game.logger.log(LogType.ERROR_STACKTRACE);
e.printStackTrace();
}
(The class above works, my image loads)
Is there a reason why this doesn't work? Is there a better method? (And yes, I do have it in my directory)
解决方案 Most probably because you're running this out of a jar, and there's no File
object to get. Compare how you're loading your images with getResourceAsStream
, which can find resources that are either unpacked as files (usually for development) or packaged into a jar. Use the same getResourceAsStream
call in createFont
.
这篇关于Java加载自定义字体文件(.ttf)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!