Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java2D OpenGL graphics acceleration not working

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);
    }
}
like image 859
Stefan Dollase Avatar asked Sep 10 '25 00:09

Stefan Dollase


1 Answers

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.

like image 50
Stefan Dollase Avatar answered Sep 12 '25 14:09

Stefan Dollase