/*
* Copyright (C) 2011 Alasdair C. Hamilton
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package ket.display;
import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import java.io.*;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.ActionListener;
import ket.math.*;
import geom.Offset;
import geom.Position;
import ketUI.Document;
import ket.display.box.Box;
import ket.display.ColourScheme;
import ket.display.ColourSchemeDecorator;
import ketUI.Ket;
/**
* This class provides a number of helper methods to save images and to create
* them from boxes.
*/
public class ImageTools {
public static BufferedImage paintInnerBoxToImage(Box box, boolean band, ColourScheme colourScheme) {
int width = (int) box.getInnerRectangle().width;
int height = (int) box.getInnerRectangle().height;
if (width==0 || height==0) {
return null;
}
BufferedImage image = new BufferedImage(width+1, height+1, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2D = (Graphics2D) image.getGraphics();
setHints(g2D);
Position topLeft = new Position(0, 0);
double boxXPos = box.getXPosition(topLeft);
double boxYPos = box.getYPosition(topLeft);
if (band) {
box.paintBand(g2D, new Position(-boxXPos, -boxYPos), colourScheme);
} else {
box.paint(g2D, new Position(-boxXPos, -boxYPos), colourScheme);
}
return image;
}
public static void setHints(Graphics2D g2D) {
g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2D.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2D.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
}
/**
* Draw a box in an image of the dimensions width by height.
*/
public static BufferedImage boxToBufferedImage(Box box, int width, int height, ColourScheme colourScheme) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2D = (Graphics2D) image.getGraphics();
setHints(g2D);
box.setupAndPaint(g2D, colourScheme, Box.DEFAULT_BOX_FONT_SIZE, new Position(0.0, 0.0), new Offset(width, height), true);
return image;
}
/**
* Draw a box in an image of the dimensions width by height.
*/
public static BufferedImage boxToBufferedImage(Box box, int width, int height, ColourScheme colourScheme, boolean clear) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2D = (Graphics2D) image.getGraphics();
setHints(g2D);
box.setupAndPaint(g2D, colourScheme, Box.DEFAULT_BOX_FONT_SIZE, new Position(0.0, 0.0), new Offset(width, height), clear);
return image;
}
/**
* Convert a box into a buffered image the size of the box's inner
* rectangle.
*/
public static BufferedImage boxToBufferedImage(Box box, ColourScheme colourScheme) {
box.setupInnerRectangle(box.getFontSize());
Offset innerRectangle = box.getInnerRectangle();
Offset imageRectangle = new Offset(innerRectangle);
BufferedImage image = new BufferedImage((int) innerRectangle.width, (int) innerRectangle.height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2D = (Graphics2D) image.getGraphics();
setHints(g2D);
box.setupOuterRectangle(imageRectangle);
Position topLeft = new Position(0.0, 0.0);
box.paint(g2D, topLeft, colourScheme);
return image;
}
/**
* Save a (png) image to the given filename. When IOExceptions are
* thrown, they are caught internally and the method returns false.
*/
public static boolean saveImage(BufferedImage image, String imageName) {
try {
ImageIO.write(image, "png", new File(imageName));
return true;
} catch (IOException ioe) {
Ket.out.println(" !!! Can't save image '"+imageName+"' !!! ");
Ket.out.println(ioe);
return false;
}
}
public static ImageIcon getIcon(String imageName, String altText) {
String imgLocation = "data/"+imageName+".png";
URL imageURL = Document.class.getClassLoader().getResource(imgLocation);
if (imageURL==null) {
System.err.println(" !!! Image for icon '" + imageName + "' not found at path '" + imageName + "' !!! ");
return null;
}
return new ImageIcon(imageURL, altText);
}
public static JButton makeNavigationButton(String imageName, String toolTipText, String altText, ActionListener actionListener) {
JButton button = new JButton();
button.setToolTipText(toolTipText);
button.setFocusable(false);
button.addActionListener(actionListener);
ImageIcon imageIcon = ImageTools.getIcon("grey/"+imageName, altText);
if (imageIcon!=null) {
button.setIcon(imageIcon);
} else {
button.setText(altText);
}
ImageIcon altIcon = ImageTools.getIcon("black/"+imageName, altText);
if (altIcon!=null) {
button.setRolloverIcon(altIcon);
}
return button;
}
}