01: package com.xoetrope.export.image;
02:
03: import com.xoetrope.carousel.build.BuildProperties;
04: import java.awt.Dimension;
05: import java.awt.Graphics;
06: import java.awt.image.BufferedImage;
07: import java.io.File;
08: import java.io.IOException;
09: import javax.imageio.ImageIO;
10: import javax.swing.JComponent;
11: import net.xoetrope.debug.DebugLogger;
12:
13: /**
14: * A helper class for exporting image file
15: *
16: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
17: * the GNU Public License (GPL), please see license.txt for more details. If
18: * you make commercial use of this software you must purchase a commercial
19: * license from Xoetrope.</p>
20: * <p> $Revision: 1.2 $</p>
21: */
22: public class XImageExportHelper {
23: /**
24: * Write a PNG image to file
25: * @param c the component to write (via its print method)
26: * @param fileName the name of the file to write, or < 0 to use the component's current size
27: * @param w the width of the image in pixels
28: * @param h the height of the image in pixels
29: */
30: public static void writePng(JComponent c, String fileName, int w,
31: int h) {
32: if (!fileName.endsWith(".png"))
33: fileName += ".png";
34: writeImage(c, fileName, "png", w, h);
35: }
36:
37: /**
38: * Write an image to file
39: * @param c the component to write (via its print method)
40: * @param fileName the name of the file to write
41: * @param flavor the file flavour, "png" for png files, or jpeg, gif, bmp, wbmp
42: * @param w the width of the image in pixels, or < 0 to use the component's current size
43: * @param h the height of the image in pixels
44: */
45: public static void writeImage(JComponent c, String fileName,
46: String flavor, int w, int h) {
47: if (w < 0) {
48: Dimension d = c.getSize();
49: w = d.width;
50: h = d.height;
51: }
52: BufferedImage image = new BufferedImage(w, h,
53: BufferedImage.TYPE_INT_RGB);
54: Graphics g = image.createGraphics();
55: c.print(g);
56: g.dispose();
57:
58: try {
59: File f = new File(fileName);
60: ImageIO.write(image, flavor, f);
61: } catch (IOException ioe) {
62: if (BuildProperties.DEBUG)
63: DebugLogger.logError("Unable to write file: "
64: + fileName + ", error " + ioe);
65: }
66: }
67: }
|