Reputation: 345
So I'm trying to make a method that returns a font object - deriving it from a .ttf, but my code isn't working:
public Font loadFont(){
Font font = null;
try {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
font = Font.createFont(Font.TRUETYPE_FONT, new File("vgafix.ttf"));
} catch (Exception e) {
//Handle exception
}
return font;
}
How would I fix this?
Upvotes: 0
Views: 98
Reputation: 347314
it's just not drawing anything. If, instead of setting the font to null, i set it to a java default font, it works
When created the default Font
size is 1
. Try using something like
Font font = loadFont().deriveFont(12f);
to set the desired size of the font
the file is in the same dir as the rest of the classfiles
You may also find that using File
to reference the font when it resides within the context of the application may mean that the file can not be found. When loading embedded resources you should use something more like Class#getResource
or Class#getResourceAsStream
, depending on your needs, for example
font = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream("/package/path/to/resource/vgafix.ttf"));
Upvotes: 1