001    /*
002     * Copyright (C) 2008-2010 by Holger Arndt
003     *
004     * This file is part of the Universal Java Matrix Package (UJMP).
005     * See the NOTICE file distributed with this work for additional
006     * information regarding copyright ownership and licensing.
007     *
008     * UJMP is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU Lesser General Public License as
010     * published by the Free Software Foundation; either version 2
011     * of the License, or (at your option) any later version.
012     *
013     * UJMP is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016     * GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public
019     * License along with UJMP; if not, write to the
020     * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
021     * Boston, MA  02110-1301  USA
022     */
023    
024    package org.ujmp.gui.io;
025    
026    import java.awt.Component;
027    import java.awt.Graphics2D;
028    import java.awt.image.BufferedImage;
029    import java.io.File;
030    import java.io.FileOutputStream;
031    import java.io.OutputStream;
032    import java.util.Iterator;
033    import java.util.logging.Level;
034    import java.util.logging.Logger;
035    
036    import javax.imageio.ImageIO;
037    import javax.imageio.ImageWriter;
038    import javax.imageio.stream.ImageOutputStream;
039    
040    public abstract class ExportPNG {
041    
042            private static final Logger logger = Logger.getLogger(ExportPNG.class.getName());
043    
044            public static final void save(String file, Component c) {
045                    save(file, c, c.getWidth());
046            }
047    
048            public static final void save(File file, Component c) {
049                    save(file, c, c.getWidth());
050            }
051    
052            public static final void save(String file, Component c, int width) {
053                    save(new File(file), c, width);
054            }
055    
056            public static final void save(File file, Component c, int width) {
057                    if (file == null) {
058                            logger.log(Level.WARNING, "no file selected");
059                            return;
060                    }
061                    if (c == null) {
062                            logger.log(Level.WARNING, "no component provided");
063                            return;
064                    }
065                    double factor = width / c.getWidth();
066                    BufferedImage image = new BufferedImage((int) (c.getWidth() * factor), (int) (c.getHeight() * factor),
067                                    BufferedImage.TYPE_INT_RGB);
068                    Graphics2D g2 = image.createGraphics();
069                    g2.scale(factor, factor);
070                    c.paint(g2);
071                    try {
072                            OutputStream out = new FileOutputStream(file);
073                            Iterator<?> writers = ImageIO.getImageWritersByFormatName("png");
074                            ImageWriter writer = (ImageWriter) writers.next();
075                            ImageOutputStream ios = ImageIO.createImageOutputStream(file);
076                            writer.setOutput(ios);
077                            writer.write(image);
078                            out.close();
079                    } catch (Exception e) {
080                            logger.log(Level.WARNING, "could not save image", e);
081                    }
082            }
083    
084    }