BufferStrategy
类表示用来在特定的
Canvas
或
Window
上组织复杂内存的机制。硬件和软件限制决定了是否能够实现特定的缓冲区策略以及如何实现它。从创建
Canvas
或
Window
时所用
GraphicsConfiguration
的性能中可以发觉这些限制。
值得注意的是,术语
buffer
和
surface
意思相同:视频设备内存或系统内存中的连续内存区域。
存在几种类型的复杂缓冲区策略,包括连续环形缓冲和 blit 缓冲。连续环形缓冲(即双缓冲或三次缓冲)是最常见的缓冲区策略;将一个应用程序绘制到单个
后备缓冲区
,然后只用一个步骤将其内容移入到前端(显示),这可通过复制数据或移动视频指针完成。移动视频指针可交换缓冲区,于是绘制的第一个缓冲区成为
前端缓冲区
,或称设备上当前所显示的内容;这称为
页面翻转
。
作为一种替代方式,可以复制后备缓冲区的内容,即使用链而不是移动视频指针进行
位图传输 (blitted)
。
*********** ***********
* * ------> * *
[到显示器] <---- * Front B * 显示 * Back B. * <---- 呈现
* * <------ * *
*********** ***********
三次缓冲:
[到 *********** *********** ***********
显示器] * * --------+---------+------> * *
<---- * Front B * 显示 * Mid. B. * * Back B. * <---- 呈现
* * <------ * * <----- * *
*********** *********** ***********
以下是一个如何创建和使用缓冲区策略的例子:
// Check the capabilities of the GraphicsConfiguration
// Create our component
Window w = new Window(gc);
// Show our window
w.setVisible(true);
// Create a general double-buffering strategy
w.createBufferStrategy(2);
BufferStrategy strategy = w.getBufferStrategy();
// Main loop
while (!done) {
// Prepare for rendering the next frame
// ...
// Render single frame
// The following loop ensures that the contents of the drawing buffer
// are consistent in case the underlying surface was recreated
// Get a new graphics context every time through the loop
// to make sure the strategy is validated
Graphics graphics = strategy.getDrawGraphics();
// Render to graphics
// ...
// Dispose the graphics
graphics.dispose();
// Repeat the rendering if the drawing buffer contents
// were restored
} while (strategy.contentsRestored());
// Display the buffer
strategy.show();
// Repeat the rendering if the drawing buffer was lost
} while (strategy.contentsLost());
// Dispose the window
w.setVisible(false);
w.dispose();
abstract
BufferCapabilities
getCapabilities
()
返回此
BufferStrategy
的
BufferCapabilities
。
abstract
Graphics
getDrawGraphics
()
创建用于绘制缓冲区的图形上下文。
abstract void
show
()
通过复制内存(位图传输)或更改显示指针(翻转)使下一个可用缓冲区可见。
返回上次调用
getDrawGraphics
后绘制缓冲区是否丢失。由于使用缓冲区策略的缓冲区通常是
VolatileImage
类型的,因此它们有可能丢失。有关对丢失缓冲区的讨论,请参阅
VolatileImage
。
返回绘制缓冲区最近是否从丢失状态恢复,并重新初始化为默认背景色(白色)。由于使用缓冲区策略的缓冲区通常是
VolatileImage
类型的,因此它们有可能丢失。如果上次调用
getDrawGraphics
后,缓冲区最近已从丢失状态恢复,则缓冲区可能要求重新绘制。有关对丢失缓冲区的讨论,请参阅
VolatileImage
。
释放当前由此
BufferStrategy
使用的系统资源,并从关联 Component 中移除它们。在调用此方法之后,
getBufferStrategy
将返回 null。试图在释放
BufferStrategy
后使用它将导致不确定的行为。