- java.lang.Object
-
- java.awt.image.BufferStrategy
-
public abstract class BufferStrategy extends Object
BufferStrategy类表示在特定的Canvas或Window上组织复杂内存的机制。 硬件和软件的限制决定了是否以及如何实现特定的缓冲策略。 通过在创建Canvas或Window时使用的GraphicsConfiguration的功能可以检测到这些限制。值得注意的是, 缓冲区和表面术语意在同义:在视频设备存储器或系统存储器中连续存储器的区域。
有几种类型的复杂缓冲策略,包括顺序环形缓冲和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
- 另请参见:
-
Window,Canvas,GraphicsConfiguration,VolatileImage
-
-
构造方法摘要
构造方法 Constructor 描述 BufferStrategy()
-
方法摘要
所有方法 接口方法 抽象方法 具体的方法 Modifier and Type 方法 描述 abstract booleancontentsLost()返回自上次调用getDrawGraphics以来绘图缓冲区是否丢失。abstract booleancontentsRestored()返回绘图缓冲区是否最近从丢失状态恢复并重新初始化为默认背景颜色(白色)。voiddispose()释放此BufferStrategy目前使用的系统资源,并将其从关联的组件中删除。abstract BufferCapabilitiesgetCapabilities()返回BufferCapabilities为这个BufferStrategy。abstract GraphicsgetDrawGraphics()为绘图缓冲区创建图形上下文。abstract voidshow()通过复制内存(blitting)或更改显示指针(翻转),使下一个可用的缓冲区可见。
-
-
-
方法详细信息
-
getCapabilities
public abstract BufferCapabilities getCapabilities()
返回BufferCapabilities此BufferStrategy。- 结果
- 这个策略的缓冲功能
-
getDrawGraphics
public abstract Graphics getDrawGraphics()
为绘图缓冲区创建图形上下文。 出于性能原因,此方法可能无法同步; 多线程使用此方法应在应用程序级别处理。 获取的图形对象的处理必须由应用程序处理。- 结果
- 绘图缓冲区的图形上下文
-
contentsLost
public abstract boolean contentsLost()
返回自上次调用getDrawGraphics以来绘图缓冲区是否丢失。 由于缓冲区策略中的缓冲区通常是类型为VolatileImage,它们可能会丢失。 有关丢失缓冲区的讨论,请参阅VolatileImage。- 结果
-
自上次调用
getDrawGraphics以来,绘图缓冲区是否丢失。 - 另请参见:
-
VolatileImage
-
contentsRestored
public abstract boolean contentsRestored()
返回绘图缓冲区是否最近从丢失状态恢复并重新初始化为默认背景颜色(白色)。 由于缓冲区策略中的缓冲区通常是类型为VolatileImage,它们可能会丢失。 如果自上次调用getDrawGraphics以来,表面最近已经从丢失状态恢复,则可能需要重新绘制。 有关丢失缓冲区的讨论,请参阅VolatileImage。- 结果
-
自上次调用
getDrawGraphics以来,绘图缓冲区是否已恢复。 - 另请参见:
-
VolatileImage
-
show
public abstract void show()
通过复制内存(blitting)或更改显示指针(翻转),使下一个可用的缓冲区可见。
-
dispose
public void dispose()
释放此BufferStrategy目前使用的系统资源,并将其从关联的组件中删除。 调用此方法后,getBufferStrategy将返回null。 在BufferStrategy之后尝试使用BufferStrategy将导致未定义的行为。
-
-