[英]Importing Fonts from JAR file
我試圖使用以下方法創建自定義字體,但是會引發異常:
流關閉
並沒有任何反應! 如何從JAR導入ttf文件並在Java 2D中使用它! 我設法使其與外部文件一起使用,但是它與InputStream一起不起作用!
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;
}
您缺少斜杠。 沒有它,它將相對於進行調用的類進行搜索。 嘗試:
this.getClass().getResourceAsStream("/com/or/dungeon/" + filename));
或者,嘗試:
this.getClass().getClassLoader().getResourceAsStream("com/or/dungeon/" + filename));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.