模块 java.desktop

类 BufferStrategy

java.lang.Object
java.awt.image.BufferStrategy
已知子类:
Component.BltBufferStrategy , Component.FlipBufferStrategy

public abstract class BufferStrategy extends Object
BufferStrategy 类表示在特定 CanvasWindow 上组织复杂内存的机制。硬件和软件限制决定了特定缓冲策略是否可以实施以及如何实施。这些限制可通过创建 CanvasWindow 时使用的 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
参见:
  • 构造方法总结

    构造方法
    修饰符
    构造方法
    描述
    protected
    子类调用的构造方法。
  • 方法总结

    修饰符和类型
    方法
    描述
    abstract boolean
    返回自上次调用 getDrawGraphics 以来绘图缓冲区是否丢失。
    abstract boolean
    返回绘图缓冲区最近是否从丢失状态恢复并重新初始化为默认背景颜色(白色)。
    void
    释放此 BufferStrategy 当前消耗的系统资源,并将其从关联的组件中删除。
    返回此 BufferStrategyBufferCapabilities
    abstract Graphics
    为绘图缓冲区创建图形上下文。
    abstract void
    show()
    通过复制内存(blitting)或更改显示指针(翻转)使下一个可用缓冲区可见。

    在类 java.lang.Object 中声明的方法

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 构造方法详细信息

    • BufferStrategy

      protected BufferStrategy()
      子类调用的构造方法。
  • 方法详情

    • getCapabilities

      public abstract BufferCapabilities  getCapabilities()
      返回此 BufferStrategyBufferCapabilities
      返回:
      该策略的缓冲能力
    • getDrawGraphics

      public abstract Graphics  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
      参见: