Java2D OpenGL graphics acceleration not working

倾然丶 夕夏残阳落幕 提交于 2019-12-13 19:22:26

问题


I want to use Swing together with the Java2D OpenGL graphics acceleration. However, it does not work.

I answered this by myself, since I searched for the solution for quite some time.

Here is my code:

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class OpenGLTest {
    public static void main(String[] args) throws ClassNotFoundException,
            InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
        // set system look and feel
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

        // activate opengl
        System.setProperty("sun.java2d.opengl", "true");

        // create and show the GUI in the event dispatch thread
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame();
        frame.setTitle("OpenGL Test");
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

回答1:


Problem

The problem with the above code is, that it interacts with a Swing class before the property "sun.java2d.opengl" is set to "true". Setting the look and feel already counts as such an interaction.

Verifying the Problem

You can see this by setting the property "sun.java2d.opengl" to "True" instead of "true". As described in the Java2D Properties Guide, this causes Java to output the following message to the console when it activates OpenGL graphics acceleration:

OpenGL pipeline enabled for default config on screen 0

Executing the code from the question with the property set to "True" does not output this message. This indicates that the OpenGL graphics acceleration is not activate.

Solution

To solve this, set the property before setting the look and feel.

Replace this

        // set system look and feel
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

        // activate opengl
        System.setProperty("sun.java2d.opengl", "true");

by this

        // activate opengl
        System.setProperty("sun.java2d.opengl", "True");

        // set system look and feel
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

This causes the code to display the debugging message given above, which indicates that OpenGL graphics acceleration is indeed activated.



来源:https://stackoverflow.com/questions/35641229/java2d-opengl-graphics-acceleration-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!