001/* Copyright (C) 2013 TU Dortmund 002 * This file is part of AutomataLib, http://www.automatalib.net/. 003 * 004 * AutomataLib is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU Lesser General Public 006 * License version 3.0 as published by the Free Software Foundation. 007 * 008 * AutomataLib is distributed in the hope that it will be useful, 009 * but WITHOUT ANY WARRANTY; without even the implied warranty of 010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 011 * Lesser General Public License for more details. 012 * 013 * You should have received a copy of the GNU Lesser General Public 014 * License along with AutomataLib; if not, see 015 * http://www.gnu.de/documents/lgpl.en.html. 016 */ 017package net.automatalib.commons.dotutil; 018 019import java.awt.Color; 020import java.awt.Dimension; 021import java.awt.Graphics; 022import java.awt.event.ActionEvent; 023import java.awt.image.BufferedImage; 024import java.io.File; 025import java.io.IOException; 026 027import javax.imageio.ImageIO; 028import javax.swing.AbstractAction; 029import javax.swing.Action; 030import javax.swing.JComponent; 031import javax.swing.JFileChooser; 032import javax.swing.JOptionPane; 033 034/** 035 * Component that displays a {@link BufferedImage}. 036 * 037 * @author Malte Isberner <malte.isberner@gmail.com> 038 * 039 */ 040public class ImageComponent extends JComponent { 041 private static final long serialVersionUID = -1L; 042 043 private BufferedImage img; 044 045 private boolean scale = false; 046 047 048 private final Action savePngAction = new AbstractAction("Save PNG") { 049 private static final long serialVersionUID = 1L; 050 @Override 051 public void actionPerformed(ActionEvent e) { 052 JFileChooser chooser = new JFileChooser(); 053 chooser.setFileFilter(DOTMisc.PNG_FILTER); 054 int res = chooser.showSaveDialog(ImageComponent.this); 055 if(res != JFileChooser.APPROVE_OPTION) 056 return; 057 File f = chooser.getSelectedFile(); 058 try { 059 ImageIO.write(img, "png", f); 060 } 061 catch(IOException ex) { 062 JOptionPane.showMessageDialog(ImageComponent.this, "Couldn't save image: " + ex.getMessage(), "Couldn't save image", JOptionPane.ERROR_MESSAGE); 063 } 064 } 065 }; 066 067 068 /** 069 * Default constructor. 070 */ 071 public ImageComponent() { 072 setPreferredSize(new Dimension(320, 240)); 073 } 074 075 /** 076 * Constructor. Initializes the component to display the given image. 077 * @param img the image to be displayed 078 */ 079 public ImageComponent(BufferedImage img) { 080 this.img = img; 081 Dimension dim = new Dimension(img.getWidth(), img.getHeight()); 082 setSize(dim); 083 setPreferredSize(dim); 084 } 085 086 /** 087 * Sets the image to be displayed. 088 * @param img the image to be displayed 089 */ 090 public void setImage(BufferedImage img) { 091 this.img = img; 092 Dimension dim; 093 if(img != null) 094 dim = new Dimension(img.getWidth(), img.getHeight()); 095 else 096 dim = new Dimension(320, 240); 097 098 setSize(dim); 099 setPreferredSize(dim); 100 repaint(); 101 } 102 103 /** 104 * Retrieves the image to be displayed 105 * @return the image to be displayed 106 */ 107 public BufferedImage getImage() { 108 return img; 109 } 110 111 /** 112 * Retrieves an {@link Action} to save the image in a PNG file. 113 * @return the action 114 */ 115 public Action getSavePngAction() { 116 return savePngAction; 117 } 118 119 /* 120 * (non-Javadoc) 121 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics) 122 */ 123 @Override 124 protected void paintComponent(Graphics g) { 125 g.setColor(Color.WHITE); 126 g.fillRect(0, 0, getWidth(), getHeight()); 127 if(img != null) { 128 if(!scale) 129 g.drawImage(img, 0, 0, null); 130 else 131 g.drawImage(img, 0, 0, getWidth(), getHeight(), null); 132 } 133 } 134 135 136}