如何在静态类中加载字体并用作JAR

时间:2017-03-27 17:42:38

标签: java eclipse jar

我尝试在eclipse中正常运行导出到可运行的jar文件。它没有正确启动,所以我尝试使用终端启动它,我收到了这个错误消息:

java.io.IOException: Can't read res\fonts\slkscr.ttf
        at java.awt.Font.createFont(Unknown Source)
        at dev.tilegame.gfx.FontLoader.loadFont(FontLoader.java:12)
        at dev.tilegame.gfx.Assets.init(Assets.java:21)
        at dev.tilegame.Game.init(Game.java:55)
        at dev.tilegame.Game.run(Game.java:93)
        at java.lang.Thread.run(Unknown Source)

我使用资产类来存储我的所有资产:

public static void init() {
    font28 = FontLoader.loadFont("res/fonts/slkscr.ttf", 28);
    font52 = FontLoader.loadFont("res/fonts/slkscr.ttf", 52);

那就是 FontLoader

public class FontLoader {

public static  Font loadFont(String path, float size){
    try {
        return Font.createFont(Font.TRUETYPE_FONT, new File(path)).deriveFont(Font.PLAIN, size);
    } catch (FontFormatException | IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
    return null;
}

}

-Thanks

编辑:

的EntityManager:

public class EntityManager {

private Handler handler;
private Player player;
private ArrayList<Entity> entities;
private ArrayList<Entity> postentities;
Iterator<Entity> it;
private Comparator<Entity> renderSorter = new Comparator<Entity>() {
    @Override
    public int compare(Entity a, Entity b) {
        if (a.getY() + a.getHeight() < b.getY() + b.getHeight())
            return -1;
        return 1;
    }
};

public EntityManager(Handler handler, Player player) {
    this.handler = handler;
    this.player = player;
    entities = new ArrayList<Entity>();
    postentities = new ArrayList<Entity>();
    addEntity(player);
}

public void tick() {
    Iterator<Entity> it = entities.iterator();

    while (it.hasNext()) {
        Entity e = it.next();
        e.tick();
        if (!e.isActive())
            it.remove();
    }

    for (int i = 0; i < postentities.size(); i++) {
        entities.add(postentities.get(i));
        postentities.remove(i);
    }

    entities.sort(renderSorter);
}

public void render(Graphics g) {
    for (Entity e : entities) {
        e.render(g);
    }
    player.postRender(g);
}

public void addEntity(Entity e) {
    entities.add(e);
}

public void postaddEntity(Entity e) {
    postentities.add(e);
}

2 个答案:

答案 0 :(得分:2)

您遇到的问题是,当您在JAR中有文件路径时,它是不透明网址。这意味着它不代表实际文件,而是代表压缩JAR中的路径。在您的情况下,您应该使用getResourceAsStream获取InputStream到正确的文件:

public static Font loadFont(String path, float size){
    try {
        Font f = Font.createFont(Font.TRUETYPE_FONT, FontLoader.class.getResourceAsStream(path));
        return f.deriveFont(size);
    } catch (FontFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

请注意路径应以前导斜杠开头;这表示jar文件的根目录。 (例如"res/fonts/slkscr.ttf"变为"/res/fonts/slkscr.ttf"

答案 1 :(得分:0)

而不是

return Font.createFont(Font.TRUETYPE_FONT, new File(path)).deriveFont(Font.PLAIN, size);

您可以使用

Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(new File(path))).deriveFont(Font.PLAIN,size);

FileINputStream将为您输入文件

有关FileINputStream的更多信息,请查看此内容:https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html