Search code examples
javagraphicsfontsawttruetype

TTF Font with Java AWT Graphics


Using AWT graphics, I am attempting to draw text with a .ttf font. But when drawn on the screen it only displays text in what looks to be font size 1.

Font initialization code:

font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("font.ttf"));
font.deriveFont(24F);
fontSmall = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream("font.ttf"));
fontSmall.deriveFont(16F);

And yes, those statements are inside of a try statement.

And the code that is supposed to correctly draw text with the font:

g.setFont(font);
g.setColor(RED);
g.drawString("Test123",10,10);
g.setFont(fontSmall);
g.drawString("Test123SMALL",10,10);

Thanks in advance :)


Solution

  • Try with:

    font = font.deriveFont(24F);
    

    And:

    fontSmall = fontSmall.deriveFont(16F);