1
Java / [Resolved] Font won't render if it is loaded in a different class
« on: February 26, 2014, 05:25:21 pm »
Hi,
For the sake of keeping my code tidy, I've implemented a FontLibrary class:
I've validated that non-null values are being returned, but when I try to use the FontLibrary to get fonts in a different class, the font is not rendered:
If I do the font loading in the same method as creating the Text object t, it displays as expected.
Can anyone offer any suggestions or point out my mistake?
Thanks in advance!
For the sake of keeping my code tidy, I've implemented a FontLibrary class:
public class FontLibrary {
public static Font arial = new Font();
public static Font getFont(String font){
if(font.equals("arial")){
if(arial == null){
try{
arial.loadFromFile(Paths.get("resources" + File.separatorChar + "fonts" + File.separatorChar + "arial.ttf"));
} catch(IOException e){
e.printStackTrace();
}
}
return arial;
}
else return null;
}
public static Font arial = new Font();
public static Font getFont(String font){
if(font.equals("arial")){
if(arial == null){
try{
arial.loadFromFile(Paths.get("resources" + File.separatorChar + "fonts" + File.separatorChar + "arial.ttf"));
} catch(IOException e){
e.printStackTrace();
}
}
return arial;
}
else return null;
}
I've validated that non-null values are being returned, but when I try to use the FontLibrary to get fonts in a different class, the font is not rendered:
t = new Text("Hello world!", FontLibrary.getFont("arial"), 32);
t.setPosition(0,0);
t.setColor(Color.GREEN);
window.draw(t);
t.setPosition(0,0);
t.setColor(Color.GREEN);
window.draw(t);
If I do the font loading in the same method as creating the Text object t, it displays as expected.
Can anyone offer any suggestions or point out my mistake?
Thanks in advance!