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

71 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) {
Rectangle bounds = getBounds();
int newWidth = bounds.width;
int newHeight = bounds.height;
switch (handle) {
case E, W -> newWidth += dx;
case N, S -> newHeight += dy;
case SE, NW -> {
newWidth += dx;
newHeight += dy;
}
case NE, SW -> {
newWidth += dx;
newHeight += dy;
}
}
if (newWidth < 2) newWidth = 2;
if (newHeight < 2) newHeight = 2;
this.radius = Math.max(newWidth, newHeight) / 2;
bounds.setSize(this.radius * 2, this.radius * 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;
}
}