0

I am going to convert my project into jar file. I am setting all my images and other external files with this.getClass().getResource("");

I have no problem with adding images;

xButton.setIcon(new ImageIcon(this.class.getResource("/a/b/c/d/e.png")));

But I can't register font with this method;

...
        private final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
...


try {

    ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(this.getClass().getResource("fonts/f1.ttf"))));

    // or either

    ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(MYCLASS.class.getResource("/a/b/c/d/f1.ttf"))));

    // or either

    ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File(this.class.getResource("/a/b/c/d/f1.ttf"))));

    } catch (FontFormatException e2) {
            e2.printStackTrace();
    } catch (IOException e2) {
        e2.printStackTrace();
}

Error I am getting from Eclipse is : The constructor File(URL) is undefined.

Lunatic Fnatic
  • 661
  • 2
  • 6
  • 17

1 Answers1

0

I found the solution,

I have created an InputStream and used it while registering the font to the system.

Working code is;

//code..
private final GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
final InputStream is = MYCLASS.class.getResourceAsStream("fonts/f1.ttf");
final InputStream is2 = MYCLASS.class.getResourceAsStream("fonts/f2.ttf");
//code..

try {

    ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, is));
    ge.registerFont(Font.createFont(Font.TRUETYPE_FONT,is2 ));

} catch (FontFormatException e2) {
    e2.printStackTrace();
} catch (IOException e2) {
    e2.printStackTrace();
}
Lunatic Fnatic
  • 661
  • 2
  • 6
  • 17