本文介绍了加载自定义字体时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在slick2d中加载一种字体(在eclipse中),该字体位于: resources\fonts\slkscr.ttf
并包含以下内容代码:
I am attempting to load a font, in slick2d, which (in eclipse) is located at: "resources\fonts\slkscr.ttf"
with the following code:
private void loadResources() {
try {
Font fontRaw = Font.createFont(Font.TRUETYPE_FONT,
new BufferedInputStream(Game.class.getClassLoader().
getResourceAsStream("resources/fonts/slkscr.ttf")));
Font fontBase = fontRaw.deriveFont(Font.PLAIN, 20);
this.font = new TrueTypeFont(fontBase, false);
} catch (IOException e) {
e.printStackTrace();
} catch (FontFormatException e) {
e.printStackTrace();
}
}
堆栈跟踪打印:
java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:151)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:273)
at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at java.awt.Font.createFont(Font.java:885)
at org.darestium.applications.games.game.EditorState.loadResources(EditorState.java:43)
at org.darestium.applications.games.game.EditorState.init(EditorState.java:61)
at org.darestium.applications.games.game.Game.initStatesList(Game.java:36)
at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:164)
at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)
at org.darestium.applications.games.game.Game.main(Game.java:31) java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:151)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:273)
at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
at java.io.FilterInputStream.read(FilterInputStream.java:107)
at java.awt.Font.createFont(Font.java:885)
at org.darestium.applications.games.game.EditorState.loadResources(EditorState.java:43)
at org.darestium.applications.games.game.EditorState.init(EditorState.java:61)
at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:171)
at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)
at org.darestium.applications.games.game.Game.main(Game.java:31)
Mon Jun 04 18:36:32 EST 2012 ERROR:null
关于如何防止字体是否无法加载?
Any ideas regarding how I could prevent the font from not loading?
推荐答案
private void loadResources() throws FontFormatException, IOException {
Font fontRaw = Font.createFont(Font.TRUETYPE_FONT, new File("resources/fonts/slkscr.ttf"));
Font fontBase = fontRaw.deriveFont(28f);
this.font = new TrueTypeFont(fontBase, false);
}
这篇关于加载自定义字体时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!