Search code examples
javafontsfont-sizetruetype

New java font's size is always 1


I added a new font for my project in java. However upon rendering it the text size is always at 1 it appears. The text basically appears as just a few little lines. I tried this with multiple fonts and they all did it. This is my code.

public static void intializeFonts(){       
    try{
        File font = new File("C:/The Woods/Fonts/script.ttf");
        Font scriptFontU = Font.createFont(Font.TRUETYPE_FONT, font);
        Font scriptFont = scriptFontU.deriveFont(20);
        script = new TrueTypeFont(scriptFont, false);
    } catch(Exception e){
        System.out.println("Error Loading Font");
    }            
}

This is also what I am using to render it if this helps.

    g.setFont(Fonts.script);
    g.drawString("Weight: "+ItemContainer.getWeight()+"lbs", 30, 600);

Any help would be great. Thank You.


Solution

  • When you call scriptFontU.deriveFont(20), you are calling Font.deriveFont(int). In this function, the first argument is an integer representing a style. Instead, you want to call Font.deriveFont(float), which takes a size and leaves the style unchanged. You can do this by calling scriptFontU.deriveFont(20.0), or equivalent; or call Font.deriveFont(int, float) as scriptFontU.deriveFont(Font.PLAIN, 20.0) to make it unambiguous.