added possibility to choose image paths via UI; TODO: enforce Model-View-Controller + add image downloader

This commit is contained in:
Hannes
2017-09-18 15:24:52 +02:00
parent 237aae7d5b
commit e12ae4a748
4 changed files with 239 additions and 109 deletions

View File

@@ -5,71 +5,76 @@
*/
package xkcd.construction;
import xkcd.rectangle.imagepath.PathRectangle;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import javax.imageio.ImageIO;
import xkcd.rectangle.imagepath.PathRectangle;
/**
* @author Hannes
*/
public class ImageSticher {
private final double scaleTo = 0.1D;
private static final int IMAGE_TYPE = BufferedImage.TYPE_4BYTE_ABGR;
private static final Color BACKGROUND_COLOR = Color.BLACK;
private static final double DEFAULT_SCALE = 0.1D;
private static final int IMAGE_TYPE = BufferedImage.TYPE_4BYTE_ABGR;
private static final Color BACKGROUND_COLOR = Color.BLACK;
private BufferedImage collage;
private final Collection<PathRectangle> rectangles;
private BufferedImage collage;
private final Collection<PathRectangle> rectangles;
public ImageSticher(final Collection<PathRectangle> rect) {
this.rectangles = rect;
//System.out.println("Count: " + rectangles.size());
this.initBufferedImage();
}
public ImageSticher(final Collection<PathRectangle> rect) {
this.rectangles = rect;
}
public void saveImage(final File saveFile) throws IOException {
initBufferedImage();
for (final PathRectangle rectangle : rectangles) {
try {
final BufferedImage image = loadImage(rectangle.getPath());
//System.out.println(rectangle);
collage.getGraphics().drawImage(image, (int) (rectangle.getX() * scaleTo), (int) (rectangle.getY() * scaleTo),
(int) (rectangle.getWidth() * scaleTo), (int) (rectangle.getHeight() * scaleTo), null);
} catch (final IOException ex) {
System.err.println(ex.getMessage());
}
}
ImageIO.write(collage, "png", saveFile);
}
public void saveImage(final File saveFile) throws IOException {
saveImage(saveFile, DEFAULT_SCALE);
}
private void initBufferedImage() {
int width = 0;
int height = 0;
for (final PathRectangle rectangle : rectangles) {
final int tmpWidth = (int) (rectangle.getWidth() + rectangle.getX());
final int tmpHeight = (int) (rectangle.getHeight() + rectangle.getY());
public void saveImage(final File saveFile, final double scale) throws IOException {
initBufferedImage(scale);
for (final PathRectangle rectangle : rectangles) {
try {
final BufferedImage image = loadImage(rectangle.getPath());
// System.out.println(rectangle);
collage.getGraphics().drawImage(image, (int) (rectangle.getX() * scale),
(int) (rectangle.getY() * scale), (int) (rectangle.getWidth() * scale),
(int) (rectangle.getHeight() * scale), null);
} catch (final IOException ex) {
System.err.println(ex.getMessage());
}
}
ImageIO.write(collage, "png", saveFile);
}
private void initBufferedImage(final double scale) {
int width = 0;
int height = 0;
for (final PathRectangle rectangle : rectangles) {
final int tmpWidth = (int) (rectangle.getWidth() + rectangle.getX());
final int tmpHeight = (int) (rectangle.getHeight() + rectangle.getY());
if (tmpWidth > width) {
width = tmpWidth;
}
if (tmpHeight > height) {
height = tmpHeight;
}
}
if (tmpWidth > width) {
width = tmpWidth;
}
if (tmpHeight > height) {
height = tmpHeight;
}
}
collage = new BufferedImage((int) (width * scaleTo), (int) (height * scaleTo), IMAGE_TYPE);
collage = new BufferedImage((int) (width * scale), (int) (height * scale), IMAGE_TYPE);
//collage.getGraphics().setColor(BACKGROUND_COLOR);
//collage.getGraphics().drawRect(0, 0, collage.getWidth(), collage.getHeight());
//collage.getGraphics().fillRect(0, 0, collage.getWidth(), collage.getHeight());
}
// collage.getGraphics().setColor(BACKGROUND_COLOR);
// collage.getGraphics().drawRect(0, 0, collage.getWidth(),
// collage.getHeight());
// collage.getGraphics().fillRect(0, 0, collage.getWidth(),
// collage.getHeight());
}
private BufferedImage loadImage(final File filePath) throws IOException {
return ImageIO.read(filePath);
}
private BufferedImage loadImage(final File filePath) throws IOException {
return ImageIO.read(filePath);
}
}