Java 2D Drawing Optimal Performance

前端 未结 9 1911
我寻月下人不归
我寻月下人不归 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: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.

提交回复
热议问题