Implement copy action

This commit is contained in:
2025-02-19 11:27:45 +01:00
parent b4eac668c8
commit aea90f5a93
8 changed files with 58 additions and 14 deletions

View File

@@ -48,4 +48,7 @@ public abstract class AbstractShape implements Shape {
public String toString() {
return String.format("x=%d, y=%d, width=%d, height=%d", bounds.x, bounds.y, bounds.width, bounds.height);
}
@Override
public abstract Shape clone();
}

View File

@@ -1,6 +1,7 @@
package ovh.gasser.newshapes.shapes;
import ovh.gasser.newshapes.ShapeVisitor;
import ovh.gasser.newshapes.attributes.ColorAttributes;
import ovh.gasser.newshapes.attributes.SelectionAttributes;
import java.awt.*;
@@ -19,13 +20,24 @@ public class SCircle extends AbstractShape {
visitor.visitCircle(this);
}
@Override
public Shape clone() {
var color = (ColorAttributes) getAttributes(ColorAttributes.ID);
return SCircle.create(super.getBounds().x, super.getBounds().y, this.radius, color.strokedColor);
}
public int getRadius() {
return radius;
}
public static SCircle create(int x, int y, int radius) {
return create(x, y, radius, Color.BLACK);
}
public static SCircle create(int x, int y, int radius, Color color) {
var circle = new SCircle(x, y, radius);
circle.addAttributes(new SelectionAttributes());
circle.addAttributes(new ColorAttributes(false, true, Color.BLACK, color));
return circle;
}
}

View File

@@ -12,6 +12,7 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Spliterator;
import java.util.stream.Collectors;
public class SCollection extends AbstractShape implements Streamable<Shape> {
private final static Logger logger = LoggerFactory.getLogger(SCollection.class);
@@ -43,6 +44,14 @@ public class SCollection extends AbstractShape implements Streamable<Shape> {
}
}
@Override
public Shape clone() {
var clonedChildren = children.stream().map(Shape::clone).toArray(Shape[]::new);
var collection = new SCollection(clonedChildren);
collection.addAttributes(new SelectionAttributes());
return collection;
}
@Override
public void setLoc(Point newLoc) {
final Point loc = getBounds().getLocation();
@@ -59,6 +68,10 @@ public class SCollection extends AbstractShape implements Streamable<Shape> {
return children.spliterator();
}
public void add(Shape s) {
children.add(s);
}
public void remove(Shape s) {
if (!children.remove(s)) {
logger.error("Unable to delete shape: {}", s);

View File

@@ -24,14 +24,19 @@ public class SRectangle extends AbstractShape {
'}';
}
@Override
public Shape clone() {
var color = (ColorAttributes) this.getAttributes(ColorAttributes.ID);
return SRectangle.create(super.getBounds().x, super.getBounds().y, getBounds().width, getBounds().height, color.strokedColor);
}
public static SRectangle create(int x, int y, int width, int height) {
SRectangle rect = new SRectangle(new Rectangle(x, y, width, height));
rect.addAttributes(new SelectionAttributes());
return rect;
return create(x, y, width, height, Color.BLACK);
}
public static SRectangle create(int x, int y, int width, int height, Color color) {
final SRectangle rect = create(x, y, width, height);
SRectangle rect = new SRectangle(new Rectangle(x, y, width, height));
rect.addAttributes(new SelectionAttributes());
rect.addAttributes(new ColorAttributes(false, true, Color.BLACK, color));
return rect;
}

View File

@@ -12,4 +12,5 @@ public interface Shape {
Attributes getAttributes(String key);
void addAttributes(Attributes attr);
Rectangle getBounds();
Shape clone();
}