1

The problem is that the font is constantly loading itself, and I'm wondering if there is a better work around....

I am making a simple java game and I have this code in order to draw all the objects to the frame:

public void paint(Graphics g){
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;

    Font font = null;
    try {
        font = Font.createFont(Font.TRUETYPE_FONT, new FileInputStream(new File("Font/Blocks.ttf"))).deriveFont(Font.PLAIN, 24);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (FontFormatException e) {
        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }
    g2d.setFont(font);


    //Drawing Strings
    g2d.setColor(Color.MAGENTA);
    g2d.drawString("Y CORD:" + PlayerDefine.getY(), 5, 445);
    g2d.drawString("X CORD:" + PlayerDefine.getX(), 5, 460);

Now I am getting the font to load, which is what I want, but I am seeing that every single time the frame refreshes (I have the main Timer set to 10ms) it is loading the font over and over and over again. I was wondering if there was a way to avoid this, i.e. make the font load when the game starts and it continue to use it until the window is closed. Thanks to anybody who can respond to this!

1 Answers1

2

don't try to load fonts in the paint method, do it when the class is initialised -- paint() gets called every time the object needs to render itself

Geoff Williams
  • 1,320
  • 10
  • 15