@Override
 public void flush() {
   if (gi != 0) {
     if (WinGraphicsDevice.useGDI) WinGDIGraphics2D.disposeGraphicsInfo(gi);
     else WinGDIPGraphics2D.disposeGraphicsInfo(gi);
     gi = 0;
   }
   if (surface != null) surface.dispose();
   super.flush();
 }
 @Override
 public Graphics2D createGraphics() {
   if (gi == 0) {
     if (hwnd != 0 && gc == null) {
       if (WinGraphicsDevice.useGDI)
         gi = WinGDIGraphics2D.createCompatibleImageInfo(hwnd, width, height);
       else gi = WinGDIPGraphics2D.createCompatibleImageInfo(hwnd, width, height);
     } else if (hwnd == 0 && gc != null) {
       if (WinGraphicsDevice.useGDI)
         gi =
             WinGDIGraphics2D.createCompatibleImageInfo(
                 ((WinGraphicsDevice) gc.getDevice()).getIDBytes(), width, height);
       else
         gi =
             WinGDIPGraphics2D.createCompatibleImageInfo(
                 ((WinGraphicsDevice) gc.getDevice()).getIDBytes(), width, height);
     }
   }
   if (WinGraphicsDevice.useGDI) return new WinGDIGraphics2D(this, width, height);
   return new WinGDIPGraphics2D(this, width, height);
 }
 /**
  * *************************************************************************
  *
  * <p>Constructors
  *
  * <p>*************************************************************************
  */
 public WinVolatileImage(NativeWindow nw, int width, int height) {
   if (width <= 0 || height <= 0) {
     // awt.19=Illegal size of volatile image.
     throw new IllegalArgumentException(Messages.getString("awt.19")); // $NON-NLS-1$
   }
   hwnd = nw.getId();
   this.width = width;
   this.height = height;
   if (WinGraphicsDevice.useGDI)
     gi = WinGDIGraphics2D.createCompatibleImageInfo(hwnd, width, height);
   else gi = WinGDIPGraphics2D.createCompatibleImageInfo(hwnd, width, height);
   surface = new BitmapSurface(gi, width, height);
 }
  @Override
  public int validate(GraphicsConfiguration gc) {
    if (gi != 0) {
      return IMAGE_OK;
    }

    if (WinGraphicsDevice.useGDI)
      gi =
          WinGDIGraphics2D.createCompatibleImageInfo(
              ((WinGraphicsDevice) gc.getDevice()).getIDBytes(), width, height);
    else
      gi =
          WinGDIPGraphics2D.createCompatibleImageInfo(
              ((WinGraphicsDevice) gc.getDevice()).getIDBytes(), width, height);
    return IMAGE_RESTORED;
  }
  public WinVolatileImage(WinGraphicsConfiguration gc, int width, int height) {
    if (width <= 0 || height <= 0) {
      // awt.19=Illegal size of volatile image.
      throw new IllegalArgumentException(Messages.getString("awt.19")); // $NON-NLS-1$
    }

    hwnd = 0;
    this.gc = gc;
    this.width = width;
    this.height = height;
    if (WinGraphicsDevice.useGDI)
      gi =
          WinGDIGraphics2D.createCompatibleImageInfo(
              ((WinGraphicsDevice) gc.getDevice()).getIDBytes(), width, height);
    else
      gi =
          WinGDIPGraphics2D.createCompatibleImageInfo(
              ((WinGraphicsDevice) gc.getDevice()).getIDBytes(), width, height);
    surface = new BitmapSurface(gi, width, height);
  }