- AbstractShape: make bounds protected for subclass access - STriangle: refactor resize logic - SCircle: refactor resize logic (preserves equal dimensions) - SText: add reasonable default bounds for hit testing
67 lines
2.0 KiB
Java
67 lines
2.0 KiB
Java
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.*;
|
|
|
|
public class SCircle extends AbstractShape {
|
|
|
|
private int radius;
|
|
|
|
private SCircle(int x, int y, int radius) {
|
|
super(new Rectangle(x, y, radius * 2, radius * 2));
|
|
this.radius = radius;
|
|
}
|
|
|
|
@Override
|
|
public void accept(ShapeVisitor visitor) {
|
|
visitor.visitCircle(this);
|
|
}
|
|
|
|
@Override
|
|
public void resize(ResizeHandle handle, int dx, int dy) {
|
|
int delta = Math.max(Math.abs(dx), Math.abs(dy));
|
|
|
|
boolean shrink = switch (handle) {
|
|
case E, SE, NE -> dx < 0;
|
|
case W, SW, NW -> dx > 0;
|
|
case N, S -> dy < 0;
|
|
default -> false;
|
|
};
|
|
|
|
int sizeChange = shrink ? -delta : delta;
|
|
int newWidth = bounds.width + sizeChange;
|
|
int newHeight = bounds.height + sizeChange;
|
|
|
|
if (newWidth < 2) newWidth = 2;
|
|
if (newHeight < 2) newHeight = 2;
|
|
|
|
bounds.setSize(newWidth, newHeight);
|
|
this.radius = Math.max(bounds.width, bounds.height) / 2;
|
|
}
|
|
|
|
@Override
|
|
public Shape clone() {
|
|
var color = (ColorAttributes) getAttributes(ColorAttributes.ID);
|
|
Color strokeColor = color != null ? color.strokedColor : Color.BLACK;
|
|
return SCircle.create(super.getBounds().x, super.getBounds().y, this.radius, strokeColor);
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|