Java 2D 性能缓慢 - 调整大小
我正在使用带有 Aero 的 Windows 7,并且有一个非常快的显卡 (Radeon 6870) 用于游戏.
I am using Windows 7 with Aero, and have a very fast graphics card (Radeon 6870) that I use for gaming.
在调整我用 java 制作的非常简单的程序时,我遇到了一些问题.例如,这个程序绝对什么都不做.它没有动作监听器,没有循环.它只是一个带有按钮的 GUI 界面.
I have some issues when resizing very simple programs I make with java. For instance, this program does absolutely nothing. It has no action listeners, no loops. It's simply a GUI interface with buttons.
[查看全屏]
调整组件大小大约需要一秒钟.对我来说这很明显.
It takes about a second to resize the components. For me that's very noticeable.
我已经尝试启用 OpenGL 加速来解决这个问题.我编译了 JAR 和使用 java -Dsun.java2d.opengl=true -jar C:Test.jar
运行它.结果是窗口周围的黑色区域略少,但闪烁更多.事实上,闪烁在上面的屏幕截图中显示为灰色.
I have tried to enable OpenGl acceleration to solve this problem. I compiled the JAR and
run it with java -Dsun.java2d.opengl=true -jar C:Test.jar
. The result is slightly less black areas around the window, but much more flickering. In fact the flickering shows up as grey in the screenshot above.
没有.Eclipse、Netbeans、Chrome 等应用程序已经过测试.没有人有这个问题.因此,我必须得出结论,代码一定有问题.很多人都运行过这段代码,并说他们没有问题".如果要测试它,请确保将窗口从最小尺寸调整到屏幕最大尺寸,同时以圆周运动移动鼠标.
No. Eclipse, Netbeans, Chrome and other applications have been tested. None have this issue. Therfore I must conclude that there must be some problem with the code. Various people have run this code and said they have "No issues". If you are going to test it, please make sure you resize the window from the smallest size to the largest size of the screen, whilst moving the mouse in circular motion.
import java.awt.*;
import javax.swing.*;
public class JFrameWithButtonsTest {
private int iScreen = 25;
private int iLocation = 10;
JFrame frame = new JFrame();
Container contentPane = frame.getContentPane();
public JFrameWithButtonsTest() {
JPanel northButtonPanel = new JPanel();
northButtonPanel.setLayout(new GridLayout(2,2));
northButtonPanel.add(new JButton(" I do nothing"));
northButtonPanel.add(new JButton(" I do nothing"));
northButtonPanel.add(new JButton(" I do nothing"));
northButtonPanel.add(new JButton(" I do nothing"));
contentPane.add(northButtonPanel, BorderLayout.NORTH);
JPanel southButtonPanel = new JPanel();
southButtonPanel.setLayout(new GridLayout(2,2));
southButtonPanel.add(new JButton(" I do nothing"));
southButtonPanel.add(new JButton(" I do nothing"));
southButtonPanel.add(new JButton(" I do nothing"));
southButtonPanel.add(new JButton(" I do nothing"));
contentPane.add(southButtonPanel, BorderLayout.SOUTH);
JPanel eastButtonPanel = new JPanel();
eastButtonPanel.setLayout(new GridLayout(2,2));
eastButtonPanel.add(new JButton(" I do nothing"));
eastButtonPanel.add(new JButton(" I do nothing"));
eastButtonPanel.add(new JButton(" I do nothing"));
eastButtonPanel.add(new JButton(" I do nothing"));
contentPane.add(eastButtonPanel, BorderLayout.EAST);
boolean packFrame = false;
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
frameSize.height = (int) (iScreen * screenSize.height / 100);
frameSize.width = (int) (iScreen * screenSize.width / 100);
frame.setSize(frameSize);
frame.setLocation((screenSize.width - frameSize.width) / iLocation,
(screenSize.height - frameSize.height) / iLocation);
if (packFrame) {
frame.pack();
packFrame = true;
} else {
frame.validate();
}
frame.setVisible(true);
}
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new JFrameWithButtonsTest();
}
});
}
}
请注意,如果没有此行,问题仍然存在:UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
Note that the issue is still present without this line: UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
推荐答案
我开始注意到我最近的两个 Java 程序.
I started to notice that for two of my recent Java programs.
我查看了 NetBeans 的配置文件,发现以下内容会大大改善这种情况:
I had a look at NetBeans' configuration file, and found the following would improve the situation a lot:
public static void main(final String... args) {
System.setProperty("sun.java2d.noddraw", Boolean.TRUE.toString());
...
与JVM命令行中的-Dsun.java2d.noddraw=true
相同.
Which is the same as -Dsun.java2d.noddraw=true
on the JVM command line.
相关文章