java ttf,Java加载自定义字体文件(.ttf)

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.(FileInputStream.java:146)

at java.io.FileInputStream.(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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值