package de.capacis.jzeemap;
import java.awt.Graphics2D;
import java.awt.font.TextAttribute;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.AttributedString;
import javax.imageio.ImageIO;
import javax.swing.text.AttributeSet.FontAttribute;
import org.apache.log4j.Logger;
import de.capacis.jzeemap.DrawAttributes.DaycounterAttributes;
import de.capacis.jzeemap.DrawAttributes.TimestampAttributes;
import de.capacis.jzeemap.config.v10.OutputConfiguration;
public class Image {
private static final Logger logger = Logger.getLogger(Image.class);
private final OutputConfiguration configuration;
private final BufferedImage image;
private final Graphics2D graphics;
private final DrawAttributes drawAttributes;
public Image(final OutputConfiguration conf, DrawAttributes drawAttributes, BufferedImage backgroundImage) {
this.configuration = conf;
this.drawAttributes = drawAttributes;
this.image = backgroundImage;
this.graphics = image.createGraphics();
}
public Image(final OutputConfiguration conf, DrawAttributes drawAttributes) {
this(conf, drawAttributes, new BufferedImage(conf.getImage().getWidth().intValue(), conf.getImage().getHeight().intValue(), BufferedImage.TYPE_INT_RGB));
}
public void drawPointData(SceneObject object, SceneConfiguration sceneConfig) {
final double xFactor = configuration.getRenderingArea().getWidth().intValue() / sceneConfig.getWidth();
final double yFactor = configuration.getRenderingArea().getHeight().intValue() / sceneConfig.getHeight();
final double x = ((object.getX() - sceneConfig.getMinX()) * xFactor) + configuration.getRenderingArea().getLeft().intValue();
final double y = configuration.getImage().getHeight().intValue() - (((object.getY() - sceneConfig.getMinY()) * yFactor) + configuration.getRenderingArea().getBottom().intValue());
if (x < configuration.getRenderingArea().getLeft().intValue() || x > configuration.getRenderingArea().getLeft().intValue() + configuration.getRenderingArea().getWidth().intValue()) {
logger.debug("x-clipping: ignoring " + object);
return;
}
if (y < configuration.getImage().getHeight().intValue() - configuration.getRenderingArea().getHeight().intValue() - configuration.getRenderingArea().getBottom().intValue()
|| y > configuration.getImage().getHeight().intValue() - configuration.getRenderingArea().getBottom().intValue()) {
logger.debug("y-clipping: ignoring " + object);
return;
}
Ellipse2D point = new Ellipse2D.Double(x, y, drawAttributes.getPointAttributes().getWidth(), drawAttributes.getPointAttributes().getHeight());
graphics.setColor(drawAttributes.getPointAttributes().getColor());
graphics.draw(point);
}
public void save(final File file) throws IOException {
ImageIO.write(image, configuration.getImage().getType().toString(), file);
}
public void drawTimestamp(Timestamp ts, SceneConfiguration sceneConfig) {
final TimestampAttributes att = drawAttributes.getTimestampAttributes();
if (att.isEnabled()) {
AttributedString string = new AttributedString(att.getDateFormat().get().format(ts));
string.addAttribute(TextAttribute.FONT, att.getFont());
string.addAttribute(TextAttribute.FOREGROUND, att.getColor());
graphics.drawString(string.getIterator(), att.getLeft(), configuration.getImage().getHeight().intValue() - att.getBottom());
}
}
public void drawDaycounter(int dayCount, SceneConfiguration sceneConfig) {
final DaycounterAttributes att = drawAttributes.getDaycounterAttributes();
if (att.isEnabled()) {
AttributedString string = new AttributedString(String.format(att.getFormatString(), dayCount));
string.addAttribute(TextAttribute.FONT, att.getFont());
string.addAttribute(TextAttribute.FOREGROUND, att.getColor());
graphics.drawString(string.getIterator(), att.getLeft(), configuration.getImage().getHeight().intValue() - att.getBottom());
}
}
}