问题加载自定义字体(Issue loading custom font)

2019-09-17 19:03发布

我试图加载字体,在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

任何想法有关我怎么能防止从字体不加载?

Answer 1:

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);
}


Answer 2:

把一个斜线,表示从类路径的根,可见搜索。

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

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

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

..要么..

getResource("/slkscr.ttf")

..depending在路径。



Answer 3:

你装字体都错了,你应该看看这里的例子油滑维基- True Type字体



文章来源: Issue loading custom font