equidistant stamping fix

This commit is contained in:
Patrick Haßel 2023-02-23 09:14:57 +01:00
parent 432160302e
commit 3bd07f30da
5 changed files with 35 additions and 19 deletions

View File

@ -26,24 +26,13 @@ public class DrawingPanel extends JPanel {
}
private void onPress(final MouseEvent e) {
lastPoint = new Vector(e.getPoint());
drawing.getCurrent().newRevision();
toolBox.getTool().apply(drawing.getCurrent().getCurrent(), e.getX(), e.getY());
lastPoint = toolBox.getTool().apply(null, drawing.getCurrent().getCurrent(), new Vector(e.getPoint()));
repaint();
}
private void onDrag(final MouseEvent e) {
final Vector vector = new Vector(lastPoint, e.getPoint());
int i = 0;
for (int rest = (int) Math.floor(vector.length); rest >= 20; rest -= 20) {
System.out.println(rest);
toolBox.getTool().apply(drawing.getCurrent().getCurrent(), e.getX(), e.getY());
lastPoint = lastPoint.plus(vector.withLength(20));
i++;
if (i > 100) {
break;
}
}
lastPoint = toolBox.getTool().apply(lastPoint, drawing.getCurrent().getCurrent(), new Vector(e.getPoint()));
repaint();
}

View File

@ -31,7 +31,10 @@ public class Layer {
public void newRevision() {
if (canRedo()) {
history = history.subList(0, index + 1);
final int old = history.size();
final int keep = index + 1;
history = history.subList(0, keep);
log.info("{} Revisions deleted", old - keep);
}
current = copy();
index = history.size();

View File

@ -14,7 +14,7 @@ public class Vector {
this(point.x, point.y);
}
public Vector(final Vector start, final Point end) {
public Vector(final Vector start, final Vector end) {
this(end.x - start.x, end.y - start.y);
}
@ -26,11 +26,19 @@ public class Vector {
public Vector withLength(final double newLength) {
final double factor = newLength / length;
return new Vector(x / factor, y / factor);
return new Vector(x * factor, y * factor);
}
public Vector plus(final Vector other) {
return new Vector(x + other.x, y + other.y);
}
public int intY() {
return (int) Math.round(y);
}
public int intX() {
return (int) Math.round(x);
}
}

View File

@ -1,5 +1,6 @@
package de.ph87.kindermalen.tool;
import de.ph87.kindermalen.drawing.Vector;
import lombok.Getter;
import java.awt.image.BufferedImage;
@ -13,6 +14,6 @@ public abstract class Tool {
this.name = name;
}
public abstract void apply(final BufferedImage image, final int x, final int y);
public abstract Vector apply(final Vector last, final BufferedImage image, final Vector point);
}

View File

@ -1,5 +1,6 @@
package de.ph87.kindermalen.tool.stamp;
import de.ph87.kindermalen.drawing.Vector;
import de.ph87.kindermalen.tool.Tool;
import lombok.Getter;
import lombok.ToString;
@ -91,8 +92,22 @@ public class StampTool extends Tool {
}
@Override
public void apply(final BufferedImage destination, final int x, final int y) {
destination.getGraphics().drawImage(prepared, x - prepared.getWidth() / 2, y - prepared.getHeight() / 2, null);
public Vector apply(Vector last, final BufferedImage destination, final Vector current) {
if (last == null) {
apply(destination, current);
return current;
}
final Vector vector = new Vector(last, current);
for (int rest = (int) Math.floor(vector.length); rest >= 20; rest -= 20) {
final Vector point = last.plus(vector.withLength(20));
apply(destination, point);
last = point;
}
return last;
}
private void apply(final BufferedImage destination, final Vector point) {
destination.getGraphics().drawImage(prepared, point.intX() - prepared.getWidth() / 2, point.intY() - prepared.getHeight() / 2, null);
}
}