我正在开发一个java游戏。在一些正常的毫秒之后,我正在重新绘制屏幕。 但如果计时器持续时间变得非常短,那么在这种情况下屏幕就会开始闪烁。
有什么办法可以避免吗?
我们可以使用多线程来避免这种情况吗???
最佳答案
查看this tutorial其中解释了如何使用加速图形模式和双缓冲来避免屏幕闪烁。关键概念是创建一个 BufferStrategy 来管理双缓冲:
// create the buffering strategy which will allow AWT
// to manage our accelerated graphics
createBufferStrategy(2);
strategy = getBufferStrategy();
然后,在渲染过程中,您从缓冲区策略中获取 Graphics 对象,使用 Graphics
执行渲染,然后调用 strategy.show()
。
// Get hold of a graphics context for the accelerated
// surface and blank it out
Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0,0,800,600);
g.dispose();
strategy.show();
关于java - 避免计时器java上的屏幕闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2101305/