[英]Java2D OpenGL graphics acceleration not working
我想將Swing與Java2D OpenGL圖形加速一起使用。 但是,它不起作用。
我一直在尋找答案,因為我已經搜索了很長時間。
這是我的代碼:
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類進行了交互。 設置外觀已經算是這種互動。
您可以通過將屬性"sun.java2d.opengl"
設置為"True"
而不是"true"
來查看此信息。 如《 Java2D屬性指南》中所述 ,這會導致Java在激活OpenGL圖形加速時向控制台輸出以下消息:
OpenGL pipeline enabled for default config on screen 0
從屬性設置為"True"
的問題中執行代碼不會輸出此消息。 這表明OpenGL圖形加速未激活。
要解決此問題,請在設置外觀之前設置屬性。
取代這個
// 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圖形加速確實已激活。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.