Java 2D Drawing Optimal Performance

前端 未结 9 1851
我寻月下人不归
我寻月下人不归 2020-12-04 09:46

I\'m in the process of writing a Java 2D game. I\'m using the built-in Java 2D drawing libraries, drawing on a Graphics2D I acquire from a BufferStrategy from a Canvas in a

相关标签:
9条回答
  • 2020-12-04 10:27

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

    0 讨论(0)
  • 2020-12-04 10:30

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

    Java2D Performance Issues

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

    0 讨论(0)
  • 2020-12-04 10:34

    A synthesis of the answers to this post, the 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 my tests, using these methods, I got a speed increase of 10x - 15x, making proper Java 2D graphics a possibility.

    0 讨论(0)
  • 2020-12-04 10:38

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

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

    0 讨论(0)
  • 2020-12-04 10:45

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

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

    "Runtime Flag For Solaris and Linux

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

    -Dsun.java2d.pmoffscreen=true/false

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

    0 讨论(0)
  • 2020-12-04 10:46

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

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

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

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

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

    0 讨论(0)
提交回复
热议问题