76 lines
1.6 KiB
Java
76 lines
1.6 KiB
Java
package de.ph87.kindermalen.drawing;
|
|
|
|
import lombok.ToString;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import javax.imageio.ImageIO;
|
|
import java.awt.*;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.time.ZonedDateTime;
|
|
import java.time.format.DateTimeFormatter;
|
|
|
|
@Slf4j
|
|
@ToString
|
|
public class Drawing {
|
|
|
|
public final int width;
|
|
|
|
public final int height;
|
|
|
|
public final ZonedDateTime created = ZonedDateTime.now();
|
|
|
|
@ToString.Exclude
|
|
private final Layer current;
|
|
|
|
public Drawing(final int width, final int height) {
|
|
this.width = width;
|
|
this.height = height;
|
|
current = new Layer(width, height);
|
|
log.info("New Drawing: {}", this);
|
|
}
|
|
|
|
public Graphics2D getGraphics() {
|
|
return (Graphics2D) current.getCurrent().getGraphics();
|
|
}
|
|
|
|
public void publish() {
|
|
current.publish();
|
|
}
|
|
|
|
public void undo() {
|
|
current.undo();
|
|
}
|
|
|
|
public void redo() {
|
|
current.redo();
|
|
}
|
|
|
|
public void newRevision() {
|
|
current.newRevision();
|
|
}
|
|
|
|
public void save(final File dir) throws IOException {
|
|
final int revision = current.getRevision();
|
|
final BufferedImage image = current.getCurrent();
|
|
final File subdir = new File(dir, created.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
|
|
final File file = new File(subdir, "%05d".formatted(revision) + ".png");
|
|
if (subdir.mkdirs()) {
|
|
log.debug("Directory created: {}", subdir);
|
|
}
|
|
new Thread(() -> {
|
|
try {
|
|
ImageIO.write(image, "PNG", file);
|
|
} catch (IOException e) {
|
|
log.error(e.toString());
|
|
}
|
|
}).start();
|
|
}
|
|
|
|
public Image getImage() {
|
|
return current.getCurrent();
|
|
}
|
|
|
|
}
|