Canvas initial delay issue

Renato Rodrigues rros at
Mon May 12 15:40:14 UTC 2014

Hi all,

I'm trying to convert some Java2D code to JavaFX and I'm stuck with an
issue regarding the performance of the JavaFX Canvas. At some point, I'll
have to draw thousands of small circles on the screen.

My problem is on the first drawing, on which my code takes a lot of time to
execute. But if I have to perform a second drawing, it takes only a
fraction of the time to draw (it is at least 10 times faster).

Is there anything I'm doing wrong? Is there any way to prevent that initial

I wrote this code to test it. In this code I draw 500,000 circles at random
positions on a 1000 x 1000 canvas (built previously). I linked this code to
a button click event, and on the first time I click it takes 10 seconds to
execute. But if I just click again, it takes only 0.025 seconds.

private void paintCanvas() {
    long initTime = System.currentTimeMillis();

    GraphicsContext cg = canvas.getGraphicsContext2D();
    cg.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
    cg.setFill(Color.rgb(0, 0, 0, 0.1));

    Random rand = new Random();
    for (int i = 0; i < 500000; i++) {
        cg.fillOval(1000 * rand.nextFloat(), 1000 * rand.nextFloat(), 2, 2);

    long endTime = System.currentTimeMillis();
    System.out.println("Time spent on drawing:" + (endTime -
initTime)/1000.0f);        }

Can anyone help me to understand the reason of this initial paint delay?

Thanks in advance,

*Renato Rodrigues Oliveira da Silva*
VICG - Visualization, Imaging and Computer Graphics - ICMC - USP São Carlos

More information about the openjfx-dev mailing list