/*
* 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.box;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.geom.*;
import geom.Offset;
import geom.Position;
import ket.math.*;
import ket.display.ColourScheme;
public class BoxImage extends Box {
public static final int THICKNESS = 2;
public static final int GUESS_SIZE = 20;
static final double GAP = 0.025;
BufferedImage image;
String name;
String caption;
Color border;
@Override
public Box cloneBox() {
return new BoxImage(getArgument(), getImage(), getName(), getCaption(), getSettings(), getBorder());
}
public BoxImage(Argument argument, BufferedImage image, String name, String caption, long settings, Color border) {
super(argument, settings);
this.image = image;
this.name = name;
this.caption = caption;
this.border = border;
}
public Color getBorder() {
return border;
}
public String getName() {
return name;
}
public String getCaption() {
return caption;
}
public BufferedImage getImage() {
return image;
}
@Override
protected void calcMinimumSize() {
innerRectangle = new Offset(image.getWidth(), image.getHeight());
}
@Override
public void setupOuterRectangle(Offset actualSize) {
double sx = actualSize.width / image.getWidth();
double sy = actualSize.height / image.getHeight();
if (sx<1.0 || sy<1.0) { // Scale to the smaller of the image dimensions.
double scale = Math.min(sx, sy);
innerRectangle = new Offset(scale*image.getWidth(), scale*image.getHeight());
} else {
innerRectangle = new Offset(image.getWidth(), image.getHeight());
}
super.setupOuterRectangle(actualSize);
}
@Override
public void draw(Graphics2D g2D, Position topLeft, ColourScheme colourScheme) {
double left = getXPosition(topLeft);
double top = getYPosition(topLeft);
double width = innerRectangle.width;
double height = innerRectangle.height;
AffineTransform t = AffineTransform.getTranslateInstance(left, top);
double sx = innerRectangle.width / image.getWidth();
double sy = innerRectangle.height / image.getHeight();
t.scale(sx, sy);
g2D.drawImage(image, t, null);
if (border!=null) {
g2D.setColor(border);
g2D.drawRect((int) left, (int) top, (int) width, (int) height);
}
}
@Override
public String toString() {
return "BoxImage: '" + name + "', \"" + caption + "\" " + super.toString() + " ";
}
}