1

The following code typically gives an output of around 1500-2000ms, which is far too long to simply draw a string in a JFrame.

public static void main(String[] args) {
    CustomFrame frame = new CustomFrame();
    frame.setVisible(true);
}

class CustomFrame extends JFrame {

public CustomFrame() {
    super();

    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    setPreferredSize(new Dimension(300, 300));

    pack();
}

@Override
public void paint(Graphics g) {
    super.paint(g);
    long s = System.currentTimeMillis();

    g.drawString("Hello", 50, 50);

    System.out.printf("Draw time: %dms\n", (System.currentTimeMillis() - s));
}

Also, when the code is run, the window loads immediately, but it will take the 1500-2000ms before the text appears. Same issue occurs for any calls involving text/font, including JButtons with text labels. Even this line:

System.out.println(g.getFont().toString());

will cause slowdowns.

If I add a second drawString call there is no pause between the first string being drawn and the second.

This leads me to believe that it's an issue with loading libraries on the first iteration, although I could be completely wrong

Any ideas as to what could be causing this/how to fix it? Been stumped for a while.

Cheers.

max batten
  • 11
  • 1

0 Answers0