Best way to load up a font

Hi there,

First of all:
I LOVED the registration quiz. I was getting frustrated. Its clever!

Anyways, I am trying to learn the best way I either load in a font from a ttf file, or create a font off of an image. Yes, I have searched the forum and gone through solutions replied to threads. One of them was loading the file and calling it through “createFont” so, if you are that guy, that caused a major difference in frames, not sure why. Seriously, became unplayable and would freeze here and there.

So, if you guys could possible teach me the best way to do fonts, that’d be great. :slight_smile:
Thanks,

  • A

Remember, you only call createFont() once, and save the Font it returns. To create Font objects of different sizes, use deriveFont().
Unplayable/freezing sounds like you were recreating the font from file every frame.

Wait, what do you mean?

I was basically setting the create font within the render method. I was using this:

Font f = FontManager.createFont("/prstart.ttf", 82);
g.setFont(f);

& again, that was the only time I used it… within the render method.

As a general rule, stuff shouldn’t really be created in the render method. Only rendering. So design your class something like this:


Font guiFont;

public void init() { // this could also perhaps be in a constructor
    guiFont = ... // do your initialization here
}

public void render() {
    g.setFont(guiFont);
    // do stuff
}

The application cycle is in phases, and fonts etc. are best created/loaded startup/initalization and then “cached” for continuous use.

Oh… duhh! I can’t believe I didn’t consider this. I’ll give it a try and let you know. Thank you!

Yep! It worked! Perfectly, although I did see a small decrease in FPS. Which is acceptable.

Thank you!