    001/* ===========================================================
    002 * Orson Charts : a 3D chart library for the Java(tm) platform
    003 * ===========================================================
    004 *
    005 * (C)opyright 2013-2016, by Object Refinery Limited.  All rights reserved.
    006 *
    007 * http://www.object-refinery.com/orsoncharts/index.html
    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 com.orsoncharts.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;
    047
    048import com.orsoncharts.Resources;
    049import com.orsoncharts.util.ArgChecks;
    050
    051/**
    052 * An action that handles saving the content of a panel to a JPEG image.
    053 * <br><br>
    054 * NOTE: This class is serializable, but the serialization format is subject
    055 * to change in future releases and should not be relied upon for persisting
    056 * instances of this class.
    057 *
    058 * @since 1.2
    059 */
    060@SuppressWarnings("serial")
    061public class ExportToJPEGAction extends AbstractAction {
    062
    063    /** The panel to which this action applies. */
    064    private Panel3D panel;
    065
    066    /**
    067     * Creates a new action instance.
    068     *
    069     * @param panel  the panel ({@code null} not permitted).
    070     */
    071    public ExportToJPEGAction(Panel3D panel) {
    072        super(Resources.localString("JPG_MENU_LABEL"));
    073        ArgChecks.nullNotPermitted(panel, "panel");
    074        this.panel = panel;
    075    }
    076
    077    /**
    078     * Writes the content of the panel to a PNG image, using Java's ImageIO.
    079     *
    080     * @param e  the event.
    081     */
    082    @Override
    083    public void actionPerformed(ActionEvent e) {
    084        JFileChooser fileChooser = new JFileChooser();
    085        FileNameExtensionFilter filter = new FileNameExtensionFilter(
    086                Resources.localString("JPG_FILE_FILTER_DESCRIPTION"), "jpg");
    087        fileChooser.addChoosableFileFilter(filter);
    088        fileChooser.setFileFilter(filter);
    089
    090        int option = fileChooser.showSaveDialog(this.panel);
    091        if (option == JFileChooser.APPROVE_OPTION) {
    092            String filename = fileChooser.getSelectedFile().getPath();
    093            if (!filename.endsWith(".jpg")) {
    094                filename = filename + ".jpg";
    095            }
    096            Dimension2D size = this.panel.getSize();
    097            int w = (int) size.getWidth();
    098            int h = (int) size.getHeight();
    099            BufferedImage image = new BufferedImage(w, h,
    100                    BufferedImage.TYPE_INT_RGB);
    101            Graphics2D g2 = image.createGraphics();
    102            this.panel.getDrawable().draw(g2, new Rectangle(w, h));
    103            try {
    104                ImageIO.write(image, "jpeg", new File(filename));
    105            } catch (IOException ex) {
    106                throw new RuntimeException(ex);
    107            }
    108        }
    109    }
    110
    111}