[ACCEPTED]-Java 2D Drawing Optimal Performance-java-2d

Accepted answer
Score: 34

A synthesis of the answers to this post, the 4 answers to Consty's, and my own research:

What works:

  • Use GraphicsConfiguration.createCompatibleImage to create images compatible with what you're drawing on. This is absolutely essential!
  • Use double-buffered drawing via Canvas.createBufferStrategy.
  • Use -Dsun.java2d.opengl=True where available to speed up drawing.
  • Avoid using transforms for scaling. Instead, cache scaled versions of the images you are going to use.
  • Avoid translucent images! Bitmasked images are fine, but translucency is very expensive in Java2D.

In 3 my tests, using these methods, I got a speed 2 increase of 10x - 15x, making proper Java 1 2D graphics a possibility.

Score: 16

I'm having the same issues as you are I 5 think. Check out my post here:

Java2D Performance Issues

It shows the 4 reason for the performance degradation and 3 how to fix it. It's not guaranteed to work 2 well on all platforms though. You'll see 1 why in the post.

Score: 12

Here are some tips off the top of my head. If 20 you were more specific and what you were 19 trying to do I may be able to help more. Sounds 18 like a game, but I don't want to assume.

Only 17 draw what you need to! Don't blindly call 16 repaint() all of the time, try some of the 15 siblings like repaint(Rect) or repaint(x,y,w,h).

Be 14 very careful with alpha blending as it can 13 be an expensive operation to blending images 12 / primitives.

Try to prerender / cache as 11 much as possible. If you find yourself drawing 10 a circle the same way, over and over, consider 9 drawing in into a BufferedImage and then 8 just draw the BufferedImage. You're sacrificing 7 memory for speed (typical of games / high 6 perf graphics)

Consider using OpenGL, use 5 JOGL of LWJGL. JOGL is more Java-like whereas 4 LWJGL provides more gaming functionality 3 on top of OpenGL access. OpenGL can draw 2 orders of magnitude (with proper hardware 1 and drivers) than Swing can.

Score: 3

There's an important one which hasn't been 13 mentioned yet, I think: cache images of 12 everything you can. If you have an object 11 that is moving across the screen, and it's 10 appearance doesn't change much, draw it 9 to an image and then render the image to 8 the screen in a new position each frame. Do 7 that even if the object is a very simple 6 one - you might be surprised at how much 5 time you save. Rendering a bitmap is much 4 faster than rendering primitives.

You might 3 also want to look at setting rendering hints 2 explicitly, and turning off things like 1 antialiasing if quality considerations permit.

Score: 1

I've done some basic drawing applications 6 using Java. I haven't worked on anything 5 too graphic-intensive, but I would recommend 4 that you have a good handle on all the 'repaint' invocations. An 3 extra repaint call on a parent container 2 could double the amount of rendering your 1 doing.

Score: 1

I've been watching this question, hoping 17 someone would offer you a better answer 16 than mine.

In the meantime, I found the 15 following Sun white paper which was written after a beta 14 release of jdk 1.4. There are some interesting 13 recommendations here on fine-tuning, including 12 the runtime flags (at the bottom of the 11 article):

"Runtime Flag For Solaris and Linux

Starting with the Beta 3 10 release of the SDK, version 1.4, Java 2D 9 stores images in pixmaps by default when 8 DGA is not available, whether you are working 7 in a local or remote display environment. You 6 can override this behavior with the pmoffscreen 5 flag:

-Dsun.java2d.pmoffscreen=true/false 4

If you set this flag to true, offscreen 3 pixmap support is enabled even if DGA is 2 available. If you set this flag to false, offscreen 1 pixmap support is disabled. Disabling offscreen pixmap support can solve some rendering problems. "

Score: 0

make sure you use double buffering, draw 3 first to one big buffer in memory that you 2 then flush to screen when all drawing is 1 done.

Score: 0

There are couple of things you will need 10 to keep in mind

1) is refreshing this link shows how 9 to use swingtimers which might be a good 8 option for calling repaint. Getting repaint 7 figured out (as the previous posters have 6 said is important so you're not doing too 5 much work).

2) Make sure you're only doing 4 drawing in one thread. Updating UI from 3 mulitple threads can lead to nasty things.

3) Double 2 Buffering. This makes the rendering smoother. this site has 1 some more good info for you

Score: 0

An alternative if you don't want to go pure 5 Java 2D is to use a game library such as 4 GTGE or JGame (search for them on Google), then 3 offer easy access to graphics and also offer 2 double buffering and much simpler drawing 1 commands.

More Related questions