java.lang.Object
java.awt.image.BufferStrategy
BufferStrategy 类表示在特定 Canvas 或 Window 上组织复杂内存的机制。硬件和软件限制决定了特定缓冲策略是否可以实施以及如何实施。这些限制可通过创建 Canvas 或 Window 时使用的 GraphicsConfiguration 的功能检测到。
值得注意的是,条款buffer和表面意思是同义词:连续内存区域,在视频设备内存或系统内存中。
有几种类型的复杂缓冲策略,包括顺序环形缓冲和 blit 缓冲。顺序环形缓冲(即双缓冲或三缓冲)是最常见的;一个应用程序绘制到一个后台缓冲区然后通过复制数据或移动视频指针一步将内容移动到前面(显示)。移动视频指针交换缓冲区,以便绘制的第一个缓冲区成为前缓冲区,或设备上当前显示的内容;这就是所谓的翻页.
或者,可以复制后台缓冲区的内容,或者块状在链中向前而不是移动视频指针。
Double buffering:
*********** ***********
* * ------> * *
[To display] <---- * Front B * Show * Back B. * <---- Rendering
* * <------ * *
*********** ***********
Triple buffering:
[To *********** *********** ***********
display] * * --------+---------+------> * *
<---- * Front B * Show * Mid. B. * * Back B. * <---- Rendering
* * <------ * * <----- * *
*********** *********** ***********
以下是如何创建和使用缓冲策略的示例:
// 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
do {
// The following loop ensures that the contents of the drawing buffer
// are consistent in case the underlying surface was recreated
do {
// 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();
- 自从:
- 1.4
- 参见:
-
构造方法总结
构造方法 -
方法总结
修饰符和类型方法描述abstract boolean返回自上次调用getDrawGraphics以来绘图缓冲区是否丢失。abstract boolean返回绘图缓冲区最近是否从丢失状态恢复并重新初始化为默认背景颜色(白色)。voiddispose()释放此BufferStrategy当前消耗的系统资源,并将其从关联的组件中删除。abstract BufferCapabilities返回此BufferStrategy的BufferCapabilities。abstract Graphics为绘图缓冲区创建图形上下文。abstract voidshow()通过复制内存(blitting)或更改显示指针(翻转)使下一个可用缓冲区可见。
-
构造方法详细信息
-
BufferStrategy
protected BufferStrategy()子类调用的构造方法。
-
-
方法详情
-
getCapabilities
返回此BufferStrategy的BufferCapabilities。- 返回:
- 该策略的缓冲能力
-
getDrawGraphics
为绘图缓冲区创建图形上下文。由于性能原因,此方法可能不会同步;多个线程使用此方法应该在应用程序级别处理。获得的图形对象的处置必须由应用程序处理。- 返回:
- 绘图缓冲区的图形上下文
-
contentsLost
public abstract boolean contentsLost()返回自上次调用getDrawGraphics以来绘图缓冲区是否丢失。由于缓冲区策略中的缓冲区通常为VolatileImage类型,因此它们可能会丢失。有关丢失缓冲区的讨论,请参阅VolatileImage。- 返回:
-
自上次调用
getDrawGraphics以来绘图缓冲区是否丢失。 - 参见:
-
contentsRestored
public abstract boolean contentsRestored()返回绘图缓冲区最近是否从丢失状态恢复并重新初始化为默认背景颜色(白色)。由于缓冲区策略中的缓冲区通常为VolatileImage类型,因此它们可能会丢失。如果最近从上次调用getDrawGraphics后丢失的状态恢复了表面,则可能需要重新绘制。有关丢失缓冲区的讨论,请参阅VolatileImage。- 返回:
-
自上次调用
getDrawGraphics后绘图缓冲区是否已恢复。 - 参见:
-
show
public abstract void show()通过复制内存(blitting)或更改显示指针(翻转)使下一个可用缓冲区可见。 -
dispose
public void dispose()释放此BufferStrategy当前消耗的系统资源,并将其从关联的组件中删除。调用此方法后,getBufferStrategy将返回 null。在BufferStrategy被处置后尝试使用它会导致未定义的行为。- 自从:
- 1.6
- 参见:
-