True Type Font problem

Hi, I’m trying to import a true type font (.ttf) into my Java game. I found a method of doing this on the internet but when I load the font and then display some text using it the text is really small, or maybe it’s not displaying right it’s kind of hard to tell. I also tried deriving a new font from the original with a different size but that didn’t work (using font.deriveFont(Float size)). Anyway this is where I got my method from http://www.java2s.com/Code/Java/2D-Graphics-GUI/Loadfontfromttffile.htm and this is the important part of the code:


try {
      InputStream is = DemoFonts.class.getResourceAsStream(fName);
      font = Font.createFont(Font.TRUETYPE_FONT, is);
    } catch (Exception ex) {
      ex.printStackTrace();
      System.err.println(fName + " not loaded.  Using serif font.");
      font = new Font("serif", Font.PLAIN, 24);
    }

As the Java API for the java.awt.Font.createFont(int,InputStream) says, it creates the font with a point size of 1. You need to call Font.deriveFont(int) to specify a larger point size. That should work.


try{
      InputStream is = DemoFonts.class.getResourceAsStream(fName);
      return Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(24);
} catch (Exception ex) {
      ex.printStackTrace();
      System.err.println(fName + " not loaded.  Using serif font.");
      return new Font("Serif", Font.PLAIN, 24);
}

Thanks for the reply. I tried that but it still gave me a font with the size of 1. I also tried a couple different fonts to make sure that it wasn’t a problem with the font I’m trying to use and I got the same problem with all of them. I’m not sure why but using this works instead of just sending it the size. Thanks for sending me in the right direction, I found this method while looking at the link you gave me.


deriveFont(Font.PLAIN,24);

Glad to have helped in some way :smiley:

return Font.createFont(Font.TRUETYPE_FONT, is).deriveFont(24);

The reason to why that line doesn’t work is because you are calling Font.deriveFont(int style); instead of Font.deriveFont(float size);

You have to append an f to the parameter (to make it 24f instead of just 24) in order to call the right method.

java.awt.Font has some of the finest Sun API designs ever. :slight_smile:

Wow I can’t believe I overlooked that! Hehe :stuck_out_tongue:

Really or are you being sarcastic? :slight_smile: