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; 026import java.util.List; 027 028import javax.imageio.ImageIO; 029import javax.swing.AbstractAction; 030import javax.swing.Action; 031import javax.swing.JComponent; 032import javax.swing.JFileChooser; 033import javax.swing.JOptionPane; 034 035 036/** 037 * Component that displays a {@link BufferedImage}. 038 * 039 * @author Malte Isberner <malte.isberner@gmail.com> 040 * 041 */ 042public class ImageComponent extends JComponent { 043 private static final long serialVersionUID = -1L; 044 045 private BufferedImage img; 046 047 private boolean scale = false; 048 049 050 private final Action savePngAction = new AbstractAction("Save PNG") { 051 private static final long serialVersionUID = 1L; 052 @Override 053 public void actionPerformed(ActionEvent e) { 054 JFileChooser chooser = new JFileChooser(); 055 chooser.setFileFilter(DOTMisc.PNG_FILTER); 056 int res = chooser.showSaveDialog(ImageComponent.this); 057 if(res != JFileChooser.APPROVE_OPTION) 058 return; 059 File f = chooser.getSelectedFile(); 060 try { 061 ImageIO.write(img, "png", f); 062 } 063 catch(IOException ex) { 064 JOptionPane.showMessageDialog(ImageComponent.this, "Couldn't save image: " + ex.getMessage(), "Couldn't save image", JOptionPane.ERROR_MESSAGE); 065 } 066 } 067 }; 068 069 070 /** 071 * Default constructor. 072 */ 073 public ImageComponent() { 074 setPreferredSize(new Dimension(320, 240)); 075 } 076 077 public void listActions(List<Action> actions) { 078 actions.add(savePngAction); 079 } 080 081 /** 082 * Constructor. Initializes the component to display the given image. 083 * @param img the image to be displayed 084 */ 085 public ImageComponent(BufferedImage img) { 086 this.img = img; 087 Dimension dim = new Dimension(img.getWidth(), img.getHeight()); 088 setSize(dim); 089 setPreferredSize(dim); 090 } 091 092 /** 093 * Sets the image to be displayed. 094 * @param img the image to be displayed 095 */ 096 public void setImage(BufferedImage img) { 097 this.img = img; 098 Dimension dim; 099 if(img != null) 100 dim = new Dimension(img.getWidth(), img.getHeight()); 101 else 102 dim = new Dimension(320, 240); 103 104 setSize(dim); 105 setPreferredSize(dim); 106 repaint(); 107 } 108 109 /** 110 * Retrieves the image to be displayed 111 * @return the image to be displayed 112 */ 113 public BufferedImage getImage() { 114 return img; 115 } 116 117 /** 118 * Retrieves an {@link Action} to save the image in a PNG file. 119 * @return the action 120 */ 121 public Action getSavePngAction() { 122 return savePngAction; 123 } 124 125 /* 126 * (non-Javadoc) 127 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics) 128 */ 129 @Override 130 protected void paintComponent(Graphics g) { 131 g.setColor(Color.WHITE); 132 g.fillRect(0, 0, getWidth(), getHeight()); 133 if(img != null) { 134 if(!scale) 135 g.drawImage(img, 0, 0, null); 136 else 137 g.drawImage(img, 0, 0, getWidth(), getHeight(), null); 138 } 139 } 140 141 142 public void setScale(boolean scale) { 143 this.scale = scale; 144 repaint(); 145 } 146 147 public void toggleScale() { 148 setScale(!scale); 149 } 150}