Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Resolved] Font won't render if it is loaded in a different class  (Read 5903 times)

0 Members and 2 Guests are viewing this topic.

white_nitro0

  • Newbie
  • *
  • Posts: 6
    • View Profile
Hi,

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;
    }

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);

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!
« Last Edit: February 26, 2014, 06:28:00 pm by white_nitro0 »

pdinklag

  • Sr. Member
  • ****
  • Posts: 330
  • JSFML Developer
    • View Profile
    • JSFML Website
Re: Font won't render if it is loaded in a different class
« Reply #1 on: February 26, 2014, 06:22:53 pm »
Your font is never loaded due to your code. Your static initializer states:
public static Font arial = new Font();

arial is therefore not null. However, you only load the font if arial is null:
if(arial == null){ /* loading code */ }

Alas, the loading code is never executed. I believe what you want to do is to initialize the arial variable with null:
public static Font arial = null;

Don't forget to construct it before you load it, then.
« Last Edit: February 26, 2014, 06:25:19 pm by pdinklag »
JSFML - The Java binding to SFML.

white_nitro0

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Font won't render if it is loaded in a different class
« Reply #2 on: February 26, 2014, 06:27:40 pm »
Just came to the exact same conclusion myself, thanks for your help :)