JavaTM Platform
Standard Ed. 6

java.awt.image
类 BufferStrategy

java.lang.Object
  继承者 java.awt.image.BufferStrategy
直接已知子类:
Component.BltBufferStrategy, Component.FlipBufferStrategy

public abstract class BufferStrategy
     
extends Object

BufferStrategy 类表示用来在特定的 CanvasWindow 上组织复杂内存的机制。硬件和软件限制决定了是否能够实现特定的缓冲区策略以及如何实现它。从创建 CanvasWindow 时所用 GraphicsConfiguration 的性能中可以发觉这些限制。

值得注意的是,术语 buffersurface 意思相同:视频设备内存或系统内存中的连续内存区域。

存在几种类型的复杂缓冲区策略,包括连续环形缓冲和 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
     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
另请参见:
Component, GraphicsConfiguration, VolatileImage

构造方法摘要
BufferStrategy()
           
 
方法摘要
abstract  boolean contentsLost()
          返回上次调用 getDrawGraphics 后绘制缓冲区是否丢失。
abstract  boolean contentsRestored()
          返回绘制缓冲区最近是否从丢失状态恢复,并重新初始化为默认背景色(白色)。
 void dispose()
          释放当前由此 BufferStrategy 使用的系统资源,并从关联 Component 中移除它们。
abstract  BufferCapabilities getCapabilities()
          返回此 BufferStrategyBufferCapabilities
abstract  Graphics getDrawGraphics()
          创建用于绘制缓冲区的图形上下文。
abstract  void show()
          通过复制内存(位图传输)或更改显示指针(翻转)使下一个可用缓冲区可见。
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

构造方法详细信息

BufferStrategy

public BufferStrategy()
方法详细信息

getCapabilities

public abstract BufferCapabilities getCapabilities()
返回此 BufferStrategyBufferCapabilities

返回:
此策略的缓冲性能

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()
通过复制内存(位图传输)或更改显示指针(翻转)使下一个可用缓冲区可见。


dispose

public void dispose()
释放当前由此 BufferStrategy 使用的系统资源,并从关联 Component 中移除它们。在调用此方法之后, getBufferStrategy 将返回 null。试图在释放 BufferStrategy 后使用它将导致不确定的行为。

从以下版本开始:
1.6
另请参见:
Component.createBufferStrategy(int), Component.getBufferStrategy()

JavaTM Platform
Standard Ed. 6

提交错误或意见
有关更多的 API 参考资料和开发人员文档,请参阅 Java SE 开发人员文档。该文档包含更详细的、面向开发人员的描述,以及总体概述、术语定义、使用技巧和工作代码示例。

版权所有 2007 Sun Microsystems, Inc. 保留所有权利。 请遵守许可证条款。另请参阅文档重新分发政策