3

I have used this code below, and it comes up with this stack trace:

java.io.FileNotFoundException: font.ttf (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)
    at java.io.FileInputStream.<init>(FileInputStream.java:101)
    at com.ominious.core.graphics.Assets.getFont(Assets.java:55)
    at com.ominious.core.graphics.Assets.loadImages(Assets.java:37)
    at com.ominious.core.GamePanel.init(GamePanel.java:63)
    at com.ominious.core.GamePanel.run(GamePanel.java:69)
    at java.lang.Thread.run(Thread.java:744)
Exception in thread "Thread-1" java.lang.NullPointerException
    at com.ominious.core.graphics.Assets.loadImages(Assets.java:49)
    at com.ominious.core.GamePanel.init(GamePanel.java:63)
    at com.ominious.core.GamePanel.run(GamePanel.java:69)
    at java.lang.Thread.run(Thread.java:744)

I use this code (I call the method in a resource file that I know works)

    private static Font getFont(String name) throws Exception {

    Font font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(name));

    return font;
}

And I call it here:

try {
        FONT = getFont("font.ttf");

        tileSprites = ImageIO.read(getClass().getResourceAsStream("/mom.gif"));

        SPLASH_BACKGROUND = ImageIO.read(getClass().getResourceAsStream("/swag.gif"));

    } catch (Exception e) {
        Game.logger.log(LogType.ERROR_STACKTRACE);

        e.printStackTrace();
    }

(The class above works, my image loads)

Is there a reason why this doesn't work? Is there a better method? (And yes, I do have it in my directory)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
SirTrashyton
  • 173
  • 2
  • 3
  • 12
  • Where is the font stored in relationship to the code trying to load it? – MadProgrammer Jan 18 '14 at 21:41
  • By the time of deployment, the `Font` will likely become an [tag:embedded-resource]. That being the case, the resource must be accessed by `URL` instead of `File`. See the [info page](http://stackoverflow.com/tags/embedded-resource/info) for the tag, for a way to form an `URL`. – Andrew Thompson Jan 18 '14 at 21:44

2 Answers2

6

It isn't that difficult to load a font from resources the same way you are loading the images. I know this question was asked a year ago, but i hope to finally provide an answer.

Simply use the documentation here, which details how to load custom fonts into a GraphicsEnvironment. It should look something like the following:

    GraphicsEnvironment ge = null;
    try{
      ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
      ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, game.getClass().getResourceAsStream("/fonts/fantasy.TTF")));
    } catch(FontFormatException e){} catch (IOException e){}

Note: I use classInstance.getClass().getResourceAsStream(String fileDir) to load the file from my resources directory in the Jar file.

After registering the font with the graphics environment, the font is available in calls to getAvailableFontFamilyNames() and can be used in font constructors.

Hope this answers your question finally!

Kris Rice
  • 559
  • 1
  • 5
  • 23
2

Most probably because you're running this out of a jar, and there's no File object to get. Compare how you're loading your images with getResourceAsStream, which can find resources that are either unpacked as files (usually for development) or packaged into a jar. Use the same getResourceAsStream call in createFont.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152