示例#1
0
 private static void choosePaint(Component target, Graphics2D g, boolean paintOffscreen) {
   log.debug("Painting to target " + target + " , offscreen " + paintOffscreen);
   if (paintOffscreen) {
     ((Paintable) target).paintOffscreen(g, target.getBounds());
   } else {
     target.paintAll(g);
   }
 }
示例#2
0
 /*
  *  Create a BufferedImage for AWT components.
  *
  *  @param	 component AWT component to create image from
  *  @param	 fileName name of file to be created or null
  *  @return	image the image for the given region
  *  @exception AWTException see Robot class constructors
  *  @exception IOException if an error occurs during writing
  */
 public static BufferedImage createImage(Component component, String fileName)
     throws AWTException, IOException {
   Point p = new Point(0, 0);
   SwingUtilities.convertPointToScreen(p, component);
   Rectangle region = component.getBounds();
   region.x = p.x;
   region.y = p.y;
   return ScreenCapture.createImage(region, fileName);
 }
 /**
  * A very nice trick is to center dialog with their parent.
  *
  * @param parent The parent <code>Component</code>
  * @param child The <code>Component</code> to center
  */
 public static void centerComponentChild(Component parent, Component child) {
   Rectangle par = parent.getBounds();
   Rectangle chi = child.getBounds();
   child.setLocation(
       new Point(par.x + (par.width - chi.width) / 2, par.y + (par.height - chi.height) / 2));
 }
示例#4
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";
  }