Java2D OpenGL图形加速不起作用 [英] Java2D OpenGL graphics acceleration not working
问题描述
我想将Swing与Java2D OpenGL图形加速一起使用.但是,它不起作用.
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.
这是我的代码:
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);
}
}
推荐答案
问题
上述代码的问题是,在属性"sun.java2d.opengl"
设置为"true"
之前,它与Swing类进行了交互.设置外观已经算是一种互动了.
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.
您可以通过将属性"sun.java2d.opengl"
设置为"True"
而不是"true"
来查看此内容.如《 Java2D属性指南》 所述,导致Java在激活OpenGL图形加速时向控制台输出以下消息:
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
从属性设置为"True"
的问题中执行代码不会输出此消息.这表明OpenGL图形加速未激活.
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.
要解决此问题,请先设置属性,然后再设置外观.
To solve this, set the property before setting the look and feel.
替换此
// set system look and feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
// activate opengl
System.setProperty("sun.java2d.opengl", "true");
以此
// activate opengl
System.setProperty("sun.java2d.opengl", "True");
// set system look and feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
这将导致代码显示上面给出的调试消息,这表明确实激活了OpenGL图形加速.
This causes the code to display the debugging message given above, which indicates that OpenGL graphics acceleration is indeed activated.
这篇关于Java2D OpenGL图形加速不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!