2

我正在尝试在 slick2d 中加载字体,该字体(在 Eclipse 中)位于:"resources\fonts\slkscr.ttf"使用以下代码:

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

关于如何防止字体不加载的任何想法?

4

3 回答 3

3
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);
}
于 2012-06-05T10:17:23.513 回答
1

放置一个前导斜杠表示从类路径的根目录开始搜索,vis。

..getResourceAsStream("/resources/fonts/slkscr.ttf")..

作为旁白。该stream closed消息可能告诉我们createFont需要一个可重新定位的InputStream. 请尝试:

getResource("/resources/fonts/slkscr.ttf")

..或者..

getResource("/slkscr.ttf")

...取决于路径。

于 2012-06-05T09:16:41.600 回答
0

你加载的字体都错了,你应该看看这里的例子Slick Wiki - True Type Fonts

于 2012-06-08T11:09:48.410 回答