package jkanvas.util;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.swing.JComponent;

/**
 * Saves screen-shots.
 * 
 * @author Joschi <josua.krause@googlemail.com>
 */
public final class Screenshot {

  /** The scaling factor for raster screen-shots. */
  private static final int SCALE = 3;

  /** No constructor. */
  private Screenshot() {
    throw new AssertionError();
  }

  /**
   * Ensures a directory.
   * 
   * @param dir The directory.
   */
  private static void ensureDir(final File dir) {
    if(dir == null) return;
    if(dir.isFile()) return;
    if(dir.exists()) return;
    ensureDir(dir.getParentFile());
    dir.mkdir();
  }

  /**
   * Creates an output stream for an image.
   * 
   * @param dir The directory.
   * @param prefix The name prefix.
   * @param postfix The name postfix.
   * @return The output.
   * @throws IOException I/O Exception.
   */
  private static OutputStream createOutput(final File dir, final String prefix,
      final String postfix) throws IOException {
    ensureDir(dir);
    final File out;
    int i = 0;
    for(;;) {
      final File f = new File(dir, prefix + (i++) + postfix);
      if(!f.exists()) {
        out = f;
        break;
      }
    }
    return new FileOutputStream(out);
  }

  /**
   * Saves a screen-shot as PNG.
   * 
   * @param dir The directory.
   * @param prefix The name prefix.
   * @param comp The component to draw.
   * @throws IOException I/O Exception.
   */
  public static void savePNG(final File dir, final String prefix, final JComponent comp)
      throws IOException {
    savePNG(createOutput(dir, prefix, ".png"), comp);
  }

  /**
   * Saves a screen-shot as PNG.
   * 
   * @param out The output.
   * @param comp The component.
   * @throws IOException I/O Exception.
   */
  public static void savePNG(final OutputStream out, final JComponent comp)
      throws IOException {
    final Rectangle rect = comp.getVisibleRect();
    final BufferedImage img = new BufferedImage(
        rect.width * SCALE, rect.height * SCALE, BufferedImage.TYPE_INT_ARGB);
    final Graphics2D g = img.createGraphics();
    g.scale(SCALE, SCALE);
    comp.paintAll(g);
    g.dispose();
    ImageIO.write(img, "png", out);
    out.close();
  }

}

@@ -861,45 +861,51 @@
   * 
-Creates an output stream for an image
+Finds an unused file in the given directory
 .%0A  
@@ -1034,104 +1034,74 @@
 The 
-output.%0A   * @throws IOException I/O Exception.%0A   */%0A  private static OutputStream createOutput
+file that does not yet exist.%0A   */%0A  private static File nextFile
 (fin
@@ -1133,22 +1133,16 @@
  prefix,
-%0A     
  final S
@@ -1155,35 +1155,16 @@
 postfix)
- throws IOException
  %7B%0A    e
@@ -1374,33 +1374,11 @@
 urn 
-new FileOutputStream(
 out
-)
 ;%0A  
@@ -1532,16 +1532,72 @@
 o draw.%0A
+   * @return The file that was used to store the image.%0A
    * @th
@@ -1642,36 +1642,36 @@
   public static 
-void
+File
  savePNG(final F
@@ -1758,56 +1758,116 @@
     
-savePNG(createOutput(dir, prefix, %22.png%22), comp)
+final File out = nextFile(dir, prefix, %22.png%22);%0A    savePNG(new FileOutputStream(out), comp);%0A    return out
 ;%0A  

return screenshot file