Java 2D-多缓冲区不可用
我正在尝试使用Java2D API渲染一个简单的动画。我需要双缓冲来创建平滑的动画 我试图列出可用的图形配置,如以下代码所示:Java 2D-多缓冲区不可用,java,animation,graphics,awt,Java,Animation,Graphics,Awt,我正在尝试使用Java2D API渲染一个简单的动画。我需要双缓冲来创建平滑的动画 我试图列出可用的图形配置,如以下代码所示: import java.awt.GraphicsConfiguration; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; public class GraphicsConfigurationTest { public static void main(final St
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
public class GraphicsConfigurationTest {
public static void main(final String[] args) {
final GraphicsDevice graphicsDevice
= GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
int i = 0;
for (final GraphicsConfiguration graphicsConfiguration : graphicsDevice.getConfigurations()) {
System.out.println(
i + ": multi buffer: "
+ graphicsConfiguration.getBufferCapabilities().isMultiBufferAvailable());
i++;
}
}
}
此代码打印30次false
。这是否意味着双缓冲根本不可用?或者我能把它打开吗
我在Ubuntu 16_04上运行,并使用以下命令启动这个测试类
java -Dsun.java2d.opengl=true GraphicsConfigurationTest
更新
如果我尝试显示动画,它会闪烁。我假设由于这种闪烁和isMultiBufferAvailable()
返回false
,没有使用后缓冲区
final Frame frame = new Frame(graphicsConfiguration);
frame.setUndecorated(true);
frame.setIgnoreRepaint(true);
graphicsConfiguration.getDevice().setFullScreenWindow(frame);
frame.createBufferStrategy(2);
final BufferStrategy bufferStrategy = frame.getBufferStrategy();
try {
while (/*not done*/) {
// wait few milliseconds for next frame
// render frame
do {
do {
final Graphics graphics = bufferStrategy.getDrawGraphics();
try {
// draw something using grphics
} finally {
graphics.dispose();
}
} while (bufferStrategy.contentsRestored());
bufferStrategy.show();
} while (bufferStrategy.contentsLost());
}
} finally {
graphicsConfiguration.getDevice().setFullScreenWindow(null);
frame.dispose();
}
我相信在使用AWT时,您可以使用Canvas类的缓冲策略来获得双缓冲。Swing默认是双缓冲的。我已经更新了这个问题。我试图通过调用
frame.createBufferStrategy(2)