示例#1
0
  public static String doComponentSnapshot(
      Component component,
      File file,
      SnapshotFileChooser.SnapshotFileType type,
      boolean paintOffscreen)
      throws IOException {

    // TODO Should really make this work for more components
    if (paintOffscreen && !(component instanceof Paintable)) {
      log.error("Component cannot be painted offscreen. Performing onscreen paint");
      paintOffscreen = false;
    }

    if (paintOffscreen) {

      Rectangle rect = component.getBounds();

      if (component instanceof MainPanel) {
        rect.height = ((MainPanel) component).getOffscreenImageHeight();
      } else {
        rect.height = Math.min(component.getHeight(), getMaxPanelHeight());
      }

      // translate to (0, 0) if necessary
      int dx = rect.x;
      int dy = rect.y;
      rect.x = 0;
      rect.y = 0;
      rect.width -= dx;
      rect.height -= dy;

      component.setBounds(rect);
    }

    int width = component.getWidth();
    int height = component.getHeight();

    // Call appropriate converter
    String format = null;
    String[] exts = null;
    switch (type) {
      case SVG:
        // log.debug("Exporting svg screenshot");
        exportScreenshotSVG(component, file, paintOffscreen);
        // exportScreenshotVector2D(component, file, paintOffscreen);
        break;
      case JPEG:
        format = "jpeg";
        exts = new String[] {".jpg", ".jpeg"};
        break;
      case PNG:
        format = "png";
        exts = new String[] {"." + format};
        break;
      case EPS:
        exportScreenshotEpsGraphics(component, file, paintOffscreen);
        // exportScreenshotEpsGraphicsNoRef(component, file, paintOffscreen);
        break;
    }
    if (format != null && exts != null) {
      exportScreenShotBufferedImage(component, file, width, height, exts, format, paintOffscreen);
    }
    return "OK";
  }