Files
new-shapes/src/main/java/ovh/gasser/newshapes/shapes/AbstractShape.java

89 lines
2.2 KiB
Java

package ovh.gasser.newshapes.shapes;
import ovh.gasser.newshapes.attributes.Attributes;
import java.awt.*;
import java.util.Map;
import java.util.TreeMap;
public abstract class AbstractShape implements Shape {
private final Map<String, Attributes> attributes = new TreeMap<>();
private final Rectangle bounds;
AbstractShape() {
this(null);
}
AbstractShape(Rectangle bounds) {
this.bounds = bounds;
}
@Override
public Attributes getAttributes(String key) {
return attributes.get(key);
}
@Override
public void addAttributes(Attributes attrs) {
attributes.put(attrs.getID(), attrs);
}
@Override
public void translate(int dx, int dy) {
getBounds().translate(dx, dy);
}
@Override
public void resize(ResizeHandle handle, int dx, int dy) {
Rectangle bounds = getBounds();
switch (handle) {
case E -> bounds.width += dx;
case W -> {
bounds.x += dx;
bounds.width -= dx;
}
case S -> bounds.height += dy;
case N -> {
bounds.y += dy;
bounds.height -= dy;
}
case SE -> {
bounds.width += dx;
bounds.height += dy;
}
case SW -> {
bounds.x += dx;
bounds.width -= dx;
bounds.height += dy;
}
case NE -> {
bounds.width += dx;
bounds.y += dy;
bounds.height -= dy;
}
case NW -> {
bounds.x += dx;
bounds.width -= dx;
bounds.y += dy;
bounds.height -= dy;
}
}
if (bounds.width < 1) bounds.width = 1;
if (bounds.height < 1) bounds.height = 1;
}
@Override
public Rectangle getBounds() {
return new Rectangle(this.bounds);
}
@Override
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();
}