001/* ===========================================================
002 * Orson Charts : a 3D chart library for the Java(tm) platform
003 * ===========================================================
004 * 
005 * (C)opyright 2013-2022, by David Gilbert.  All rights reserved.
006 * 
007 * https://github.com/jfree/orson-charts
008 * 
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as published by
011 * the Free Software Foundation, either version 3 of the License, or
012 * (at your option) any later version.
013 *
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 *
019 * You should have received a copy of the GNU General Public License
020 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
021 * 
022 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
023 * Other names may be trademarks of their respective owners.]
024 * 
025 * If you do not wish to be bound by the terms of the GPL, an alternative
026 * commercial license can be purchased.  For details, please see visit the
027 * Orson Charts home page:
028 * 
029 * http://www.object-refinery.com/orsoncharts/index.html
030 * 
031 */
032
033package org.jfree.chart3d.graphics3d.swing;
034
035import java.awt.Graphics2D;
036import java.awt.Rectangle;
037import java.awt.event.ActionEvent;
038import java.awt.geom.Dimension2D;
039import java.awt.image.BufferedImage;
040import java.io.File;
041import java.io.IOException;
042
043import javax.imageio.ImageIO;
044import javax.swing.AbstractAction;
045import javax.swing.JFileChooser;
046import javax.swing.filechooser.FileNameExtensionFilter;
047import org.jfree.chart3d.Resources;
048import org.jfree.chart3d.internal.Args;
049
050/**
051 * An action that handles saving the content of a panel to a PNG image.
052 * <br><br>
053 * NOTE: This class is serializable, but the serialization format is subject 
054 * to change in future releases and should not be relied upon for persisting 
055 * instances of this class. 
056 */
057@SuppressWarnings("serial")
058public class ExportToPNGAction extends AbstractAction {
059
060    /** The panel to which this action applies. */
061    private final Panel3D panel;
062  
063    /**
064     * Creates a new action instance.
065     * 
066     * @param panel  the panel ({@code null} not permitted).
067     */
068    public ExportToPNGAction(Panel3D panel) {
069        super(Resources.localString("PNG_MENU_LABEL"));
070        Args.nullNotPermitted(panel, "panel");
071        this.panel = panel;
072    }
073
074    /**
075     * Writes the content of the panel to a PNG image, using Java's ImageIO.
076     * 
077     * @param e  the event.
078     */
079    @Override
080    public void actionPerformed(ActionEvent e) {
081        JFileChooser fileChooser = new JFileChooser();
082        FileNameExtensionFilter filter = new FileNameExtensionFilter(
083            Resources.localString("PNG_FILE_FILTER_DESCRIPTION"), "png");
084        fileChooser.addChoosableFileFilter(filter);
085        fileChooser.setFileFilter(filter);
086
087        int option = fileChooser.showSaveDialog(this.panel);
088        if (option == JFileChooser.APPROVE_OPTION) {
089            String filename = fileChooser.getSelectedFile().getPath();
090            if (!filename.endsWith(".png")) {
091                filename = filename + ".png";
092            }
093            Dimension2D size = this.panel.getSize();
094            int w = (int) size.getWidth();
095            int h = (int) size.getHeight();
096            BufferedImage image = new BufferedImage(w, h, 
097                    BufferedImage.TYPE_INT_ARGB);
098            Graphics2D g2 = image.createGraphics();
099            this.panel.getDrawable().draw(g2, new Rectangle(w, h));
100            try {
101                ImageIO.write(image, "png", new File(filename));
102            } catch (IOException ex) {
103                throw new RuntimeException(ex);
104            }
105        }
106    }
107    
108}