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 PNG 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@SuppressWarnings("serial") 059public class ExportToPNGAction extends AbstractAction { 060 061 /** The panel to which this action applies. */ 062 private Panel3D panel; 063 064 /** 065 * Creates a new action instance. 066 * 067 * @param panel the panel ({@code null} not permitted). 068 */ 069 public ExportToPNGAction(Panel3D panel) { 070 super(Resources.localString("PNG_MENU_LABEL")); 071 ArgChecks.nullNotPermitted(panel, "panel"); 072 this.panel = panel; 073 } 074 075 /** 076 * Writes the content of the panel to a PNG image, using Java's ImageIO. 077 * 078 * @param e the event. 079 */ 080 @Override 081 public void actionPerformed(ActionEvent e) { 082 JFileChooser fileChooser = new JFileChooser(); 083 FileNameExtensionFilter filter = new FileNameExtensionFilter( 084 Resources.localString("PNG_FILE_FILTER_DESCRIPTION"), "png"); 085 fileChooser.addChoosableFileFilter(filter); 086 fileChooser.setFileFilter(filter); 087 088 int option = fileChooser.showSaveDialog(this.panel); 089 if (option == JFileChooser.APPROVE_OPTION) { 090 String filename = fileChooser.getSelectedFile().getPath(); 091 if (!filename.endsWith(".png")) { 092 filename = filename + ".png"; 093 } 094 Dimension2D size = this.panel.getSize(); 095 int w = (int) size.getWidth(); 096 int h = (int) size.getHeight(); 097 BufferedImage image = new BufferedImage(w, h, 098 BufferedImage.TYPE_INT_ARGB); 099 Graphics2D g2 = image.createGraphics(); 100 this.panel.getDrawable().draw(g2, new Rectangle(w, h)); 101 try { 102 ImageIO.write(image, "png", new File(filename)); 103 } catch (IOException ex) { 104 throw new RuntimeException(ex); 105 } 106 } 107 } 108 109}