So, I’ve read this article about java2D rendering and tested it. I created a pulsating red circle animation. 20x improvement over the default rendering using Java2D with Swing (From 1.500fps to 30.000fps).
I went further, enabled OpenGL and used sun.java2d.opengl.WGLSurfaceData
instead, reaching over 60.000fps…
I then compared it to C++ using GDI first (around 1.000fps) and then DirectX (around 5.000fps).
This is literally nothing compared to the hacky Java way. Is there a faster way?
Does the C++ or WindowsAPI world offer something (even hacky) to render 2D very fast?
I am not a C++ guy, but I want to learn it. What sources can I look at to learn more about efficient rendering?
Here is the code for the OpenGL-based example.
Download this to PaintFrame3.java
To compile run:
javac --add-exports=java.desktop/java.awt.peer=ALL-UNNAMED --add-exports=java.desktop/sun.awt=ALL-UNNAMED PaintFrame3.java
To execute run:
java --add-exports java.desktop/sun.java2d.windows=ALL-UNNAMED --add-exports java.desktop/sun.java2d.opengl=ALL-UNNAMED --add-exports java.desktop/java.awt.peer=ALL-UNNAMED --add-opens java.desktop/sun.awt.windows=ALL-UNNAMED --add-opens java.desktop/sun.java2d=ALL-UNNAMED --add-opens java.desktop/java.awt=ALL-UNNAMED -XX:+UseZGC PaintFrame3
I worked with Java 17. Since this is a hacky way and goes against the Java principles, it will only work on Windows systems with OpenGL capable hardware.
Any feedback, tips and explanations are appreciated.