Multiple draws or One image, more efficient?

I’ve always wondered this, since I"m not familiar with exactly what the Java2D renderer is doing. Consider something like a static star field, that you’ll generate randomly and use as a background or something. Is it more efficient to do a hundred or so draws for each star every cycle, or is it better to draw them all once to a transparent bufferedImage, then just draw the entire image each cycle?

If your BufferedImage is transparant, it is very very likely that it is much much slower than doing a few hundred fillRect(x, y, 1, 1) calls

In the end, nobody knows, as Java2D performance characteristics are rather… awkward.

I’ve found the latter true, actually. It seems that even if I draw a few rects it brings the whole thing down.

Try disabling anti-aliasing. Seriously. It makes far more of a difference than it reasonably should.

Hm. Is that a command in Graphics2D?


g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);

That appears to affect text only. Is there something similar for shapes?

RenderingHints.KEY_TEXT_ANTIALIASING
RenderingHints.KEY_ANTIALIASING

Needless to say this is disabled by default already :slight_smile:

Non anti-aliased single pixel star fields look terrible.

I say buffer the whole thing in an opaque volatile image and you should be ok.

Thanks for the replies. I just did a test. 100,000 fillRect(x,y,1,1)'s took about 35 ms. Drawing a transparent VolatileImage took 0.015 ms. Quite a difference.

You said 100 stars, not 100,000.

100 fillRect’s was about 0.04 ms. So yea, not worth dealing with a VolatileImage for something that small.