0

Trying to write a class:

private void gameLevel(Graphics g) {
  try {
     InputStream fnt_stream = getClass().getResourceAsStream("resources/iomanoid.ttf");
     Font myFont = Font.createFont(Font.TRUETYPE_FONT, fnt_stream);
     Font Iomanoid = new Font("Iomanoid", Font.BOLD, 40);

     String msg = "Level";
     g.setColor(Color.black);
     g.setFont(Iomanoid);
     g.drawString(msg, 111,111);
  } catch (Exception ex) {
     System.out.println(ex);
  }

The message appears, but not with the font specified.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
den23513
  • 3
  • 2
  • On a side note, you forgot to `close()` the input stream. Use `try-finally` or `try-with-resources`. Oh, and `fnt_stream` doesn't conform to proper Java style (only MULTI_WORD_CONSTANTS are allowed to contain underscores). – Sergei Tachenov May 03 '15 at 12:18

2 Answers2

0

You will have to register the newly created font in GraphicsEnvironment. Like this

try {
     GraphicsEnvironment ge =  GraphicsEnvironment.getLocalGraphicsEnvironment();
     ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("path/to/your/font/sampleFont.ttf"));
} catch (IOException|FontFormatException e) {
     //Handle exception
}

Take a look at here.

mushfek0001
  • 3,845
  • 1
  • 21
  • 20
0

Beside other comments

replace

Font Iomanoid = new Font("Iomanoid", Font.BOLD, 40);

by

Font iomanoid = myFont.deriveFont(Font.BOLD, 40f);

this font needs afterwards to be registered (as mentioned by mushfek0001)

For more information about fonts have a look at the Oracle tutorial about Physical and Logical Fonts

SubOptimal
  • 22,518
  • 3
  • 53
  • 69