简体   繁体   English

从JAR文件导入字体

[英]Importing Fonts from JAR file

I've attempted to create a custom font using the following method, however it throws an exception : 我试图使用以下方法创建自定义字体,但是会引发异常:

Stream closed 流关闭

and nothing happens! 并没有任何反应! How can I import a ttf file from my JAR and use it in Java 2D! 如何从JAR导入ttf文件并在Java 2D中使用它! I'v managed to get it to work with external files, but it just doesn't work with an InputStream! 我设法使其与外部文件一起使用,但是它与Inp​​utStream一起不起作用!

 public Font gameFont(String filename, float fontSize) {
    Font myfont = null;
     Font myfontReal = null;
    try {
        InputStream is = new BufferedInputStream(this.getClass().getResourceAsStream("com/or/dungeon/" + filename));

        myfont = Font.createFont(Font.TRUETYPE_FONT, is);
        myfontReal = myfont.deriveFont(fontSize);
        is.close();
    } catch (FontFormatException | IOException e) {
        System.out.println(e.getMessage());
    }
    return myfontReal;
}

You are missing a leading slash. 您缺少斜杠。 Without it, it is searching relative to the class making the call. 没有它,它将相对于进行调用的类进行搜索。 Try: 尝试:

this.getClass().getResourceAsStream("/com/or/dungeon/" + filename));

Alternatively, try: 或者,尝试:

this.getClass().getClassLoader().getResourceAsStream("com/or/dungeon/" + filename));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM