autosave
This commit is contained in:
parent
65b433b138
commit
d2c9efe9ea
@ -25,7 +25,7 @@ public class Main extends JFrame {
|
||||
|
||||
private final ToolBox toolBox = new ToolBox();
|
||||
|
||||
private final Drawing drawing = new Drawing(800, 600);
|
||||
private final Drawing drawing = new Drawing(1500, 1000);
|
||||
|
||||
private final DrawingPanel drawingPanel = new DrawingPanel(toolBox, drawing);
|
||||
|
||||
|
||||
@ -5,14 +5,21 @@ import de.ph87.kindermalen.MotionListener;
|
||||
import de.ph87.kindermalen.toolbox.ToolBox;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.time.ZonedDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
@Slf4j
|
||||
public class DrawingPanel extends JPanel {
|
||||
|
||||
private static final File SAVE_DIR = new File("./data/images");
|
||||
|
||||
private final ToolBox toolBox;
|
||||
|
||||
private final Drawing drawing;
|
||||
@ -21,10 +28,12 @@ public class DrawingPanel extends JPanel {
|
||||
|
||||
private Point cursor = null;
|
||||
|
||||
private final ZonedDateTime startup = ZonedDateTime.now();
|
||||
|
||||
public DrawingPanel(final ToolBox toolBox, final Drawing drawing) {
|
||||
this.toolBox = toolBox;
|
||||
this.drawing = drawing;
|
||||
this.addMouseListener(new ClickListener(this::onPress, null));
|
||||
this.addMouseListener(new ClickListener(this::onPress, this::onRelease));
|
||||
this.addMouseMotionListener(new MotionListener(this::onMove, this::onDrag));
|
||||
}
|
||||
|
||||
@ -45,6 +54,15 @@ public class DrawingPanel extends JPanel {
|
||||
repaint();
|
||||
}
|
||||
|
||||
private void onRelease(final MouseEvent e) {
|
||||
SAVE_DIR.mkdirs();
|
||||
try {
|
||||
ImageIO.write(drawing.getCurrent().getCurrent(), "PNG", new File(SAVE_DIR, startup.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) + ".png"));
|
||||
} catch (IOException ex) {
|
||||
log.error(ex.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paint(final Graphics g) {
|
||||
g.setColor(Color.gray);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user