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 attributes = new TreeMap<>(); protected 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) { this.bounds.translate(dx, dy); } @Override public void resize(ResizeHandle handle, int dx, int dy) { switch (handle) { case E -> this.bounds.width += dx; case W -> { this.bounds.x += dx; this.bounds.width -= dx; } case S -> this.bounds.height += dy; case N -> { this.bounds.y += dy; this.bounds.height -= dy; } case SE -> { this.bounds.width += dx; this.bounds.height += dy; } case SW -> { this.bounds.x += dx; this.bounds.width -= dx; this.bounds.height += dy; } case NE -> { this.bounds.width += dx; this.bounds.y += dy; this.bounds.height -= dy; } case NW -> { this.bounds.x += dx; this.bounds.width -= dx; this.bounds.y += dy; this.bounds.height -= dy; } } if (this.bounds.width < 1) this.bounds.width = 1; if (this.bounds.height < 1) this.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(); }