private void replaceSurfaceData(
      int newBackBufferCount, BufferCapabilities newBackBufferCaps, boolean blit) {
    synchronized (surfaceDataLock) {
      final SurfaceData oldData = getSurfaceData();
      surfaceData = platformWindow.replaceSurfaceData();
      // TODO: volatile image
      //        VolatileImage oldBB = backBuffer;
      BufferedImage oldBB = backBuffer;
      backBufferCount = newBackBufferCount;
      backBufferCaps = newBackBufferCaps;
      final Rectangle size = getSize();
      if (getSurfaceData() != null && oldData != getSurfaceData()) {
        clearBackground(size.width, size.height);
      }

      if (blit) {
        blitSurfaceData(oldData, getSurfaceData());
      }

      if (oldData != null && oldData != getSurfaceData()) {
        // TODO: drop oldData for D3D/WGL pipelines
        // This can only happen when this peer is being created
        oldData.flush();
      }

      // TODO: volatile image
      //        backBuffer = (VolatileImage)delegate.createBackBuffer();
      backBuffer = (BufferedImage) platformWindow.createBackBuffer();
      if (backBuffer != null) {
        Graphics g = backBuffer.getGraphics();
        try {
          Rectangle r = getBounds();
          if (g instanceof Graphics2D) {
            ((Graphics2D) g).setComposite(AlphaComposite.Src);
          }
          g.setColor(nonOpaqueBackground);
          g.fillRect(0, 0, r.width, r.height);
          if (g instanceof SunGraphics2D) {
            ((SunGraphics2D) g).constrain(0, 0, r.width, r.height, getRegion());
          }
          if (!isTextured()) {
            g.setColor(getBackground());
            g.fillRect(0, 0, r.width, r.height);
          }
          if (oldBB != null) {
            // Draw the old back buffer to the new one
            g.drawImage(oldBB, 0, 0, null);
            oldBB.flush();
          }
        } finally {
          g.dispose();
        }
      }
    }
    flushOnscreenGraphics();
  }
 @Override
 public void flip(int x1, int y1, int x2, int y2, BufferCapabilities.FlipContents flipAction) {
   final BufferedImage buffer = (BufferedImage) getBackBuffer();
   if (buffer == null) {
     throw new IllegalStateException("Buffers have not been created");
   }
   final Graphics g = getGraphics();
   try {
     g.drawImage(buffer, x1, y1, x2, y2, x1, y1, x2, y2, null);
   } finally {
     g.dispose();
   }
   if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {
     final Graphics2D bg = (Graphics2D) buffer.getGraphics();
     try {
       bg.setBackground(getBackground());
       bg.clearRect(0, 0, buffer.getWidth(), buffer.getHeight());
     } finally {
       bg.dispose();
     }
   }
 }
 private void clearBackground(final int w, final int h) {
   final Graphics g = getOnscreenGraphics(getForeground(), getBackground(), getFont());
   if (g != null) {
     try {
       if (g instanceof Graphics2D) {
         ((Graphics2D) g).setComposite(AlphaComposite.Src);
       }
       if (isTranslucent()) {
         g.setColor(nonOpaqueBackground);
         g.fillRect(0, 0, w, h);
       }
       if (!isTextured()) {
         if (g instanceof SunGraphics2D) {
           ((SunGraphics2D) g).constrain(0, 0, w, h, getRegion());
         }
         g.setColor(getBackground());
         g.fillRect(0, 0, w, h);
       }
     } finally {
       g.dispose();
     }
   }
 }