This commit is contained in:
Patrick Haßel 2023-02-23 10:00:15 +01:00
parent 3bd07f30da
commit ea9de93db8
4 changed files with 29 additions and 3 deletions

View File

@ -6,9 +6,12 @@ import java.util.function.Consumer;
public class MotionListener implements MouseMotionListener { public class MotionListener implements MouseMotionListener {
private final Consumer<MouseEvent> onMove;
private final Consumer<MouseEvent> onDrag; private final Consumer<MouseEvent> onDrag;
public MotionListener(final Consumer<MouseEvent> onDrag) { public MotionListener(final Consumer<MouseEvent> onMove, final Consumer<MouseEvent> onDrag) {
this.onMove = onMove;
this.onDrag = onDrag; this.onDrag = onDrag;
} }
@ -21,7 +24,9 @@ public class MotionListener implements MouseMotionListener {
@Override @Override
public void mouseMoved(final MouseEvent e) { public void mouseMoved(final MouseEvent e) {
if (onMove != null) {
onMove.accept(e);
}
} }
} }

View File

@ -8,6 +8,7 @@ import lombok.extern.slf4j.Slf4j;
import javax.swing.*; import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
@Slf4j @Slf4j
public class DrawingPanel extends JPanel { public class DrawingPanel extends JPanel {
@ -18,11 +19,18 @@ public class DrawingPanel extends JPanel {
private Vector lastPoint = null; private Vector lastPoint = null;
private Point cursor = null;
public DrawingPanel(final ToolBox toolBox, final Drawing drawing) { public DrawingPanel(final ToolBox toolBox, final Drawing drawing) {
this.toolBox = toolBox; this.toolBox = toolBox;
this.drawing = drawing; this.drawing = drawing;
this.addMouseListener(new ClickListener(this::onPress, null)); this.addMouseListener(new ClickListener(this::onPress, null));
this.addMouseMotionListener(new MotionListener(this::onDrag)); this.addMouseMotionListener(new MotionListener(this::onMove, this::onDrag));
}
private void onMove(final MouseEvent e) {
cursor = e.getPoint();
repaint();
} }
private void onPress(final MouseEvent e) { private void onPress(final MouseEvent e) {
@ -32,6 +40,7 @@ public class DrawingPanel extends JPanel {
} }
private void onDrag(final MouseEvent e) { private void onDrag(final MouseEvent e) {
cursor = e.getPoint();
lastPoint = toolBox.getTool().apply(lastPoint, drawing.getCurrent().getCurrent(), new Vector(e.getPoint())); lastPoint = toolBox.getTool().apply(lastPoint, drawing.getCurrent().getCurrent(), new Vector(e.getPoint()));
repaint(); repaint();
} }
@ -48,6 +57,11 @@ public class DrawingPanel extends JPanel {
g.setColor(Color.white); g.setColor(Color.white);
g.fillRect(0, 0, drawing.getWidth(), drawing.getHeight()); g.fillRect(0, 0, drawing.getWidth(), drawing.getHeight());
g.drawImage(drawing.getCurrent().getCurrent(), 0, 0, null); g.drawImage(drawing.getCurrent().getCurrent(), 0, 0, null);
final BufferedImage preview = toolBox.getTool().getPreview();
if (cursor != null && preview != null) {
g.drawImage(preview, cursor.x - preview.getWidth() / 2, cursor.y - preview.getHeight() / 2, null);
}
} }
} }

View File

@ -16,4 +16,6 @@ public abstract class Tool {
public abstract Vector apply(final Vector last, final BufferedImage image, final Vector point); public abstract Vector apply(final Vector last, final BufferedImage image, final Vector point);
public abstract BufferedImage getPreview();
} }

View File

@ -110,4 +110,9 @@ public class StampTool extends Tool {
destination.getGraphics().drawImage(prepared, point.intX() - prepared.getWidth() / 2, point.intY() - prepared.getHeight() / 2, null); destination.getGraphics().drawImage(prepared, point.intX() - prepared.getWidth() / 2, point.intY() - prepared.getHeight() / 2, null);
} }
@Override
public BufferedImage getPreview() {
return prepared;
}
} }