diff --git a/src/main/java/ovh/gasser/newshapes/shapes/AbstractShape.java b/src/main/java/ovh/gasser/newshapes/shapes/AbstractShape.java index b8a4a7d..0bdb9ca 100644 --- a/src/main/java/ovh/gasser/newshapes/shapes/AbstractShape.java +++ b/src/main/java/ovh/gasser/newshapes/shapes/AbstractShape.java @@ -9,7 +9,7 @@ import java.util.TreeMap; public abstract class AbstractShape implements Shape { private final Map attributes = new TreeMap<>(); - private final Rectangle bounds; + protected final Rectangle bounds; AbstractShape() { this(null); @@ -31,46 +31,45 @@ public abstract class AbstractShape implements Shape { @Override public void translate(int dx, int dy) { - getBounds().translate(dx, dy); + this.bounds.translate(dx, dy); } @Override public void resize(ResizeHandle handle, int dx, int dy) { - Rectangle bounds = getBounds(); switch (handle) { - case E -> bounds.width += dx; + case E -> this.bounds.width += dx; case W -> { - bounds.x += dx; - bounds.width -= dx; + this.bounds.x += dx; + this.bounds.width -= dx; } - case S -> bounds.height += dy; + case S -> this.bounds.height += dy; case N -> { - bounds.y += dy; - bounds.height -= dy; + this.bounds.y += dy; + this.bounds.height -= dy; } case SE -> { - bounds.width += dx; - bounds.height += dy; + this.bounds.width += dx; + this.bounds.height += dy; } case SW -> { - bounds.x += dx; - bounds.width -= dx; - bounds.height += dy; + this.bounds.x += dx; + this.bounds.width -= dx; + this.bounds.height += dy; } case NE -> { - bounds.width += dx; - bounds.y += dy; - bounds.height -= dy; + this.bounds.width += dx; + this.bounds.y += dy; + this.bounds.height -= dy; } case NW -> { - bounds.x += dx; - bounds.width -= dx; - bounds.y += dy; - bounds.height -= dy; + this.bounds.x += dx; + this.bounds.width -= dx; + this.bounds.y += dy; + this.bounds.height -= dy; } } - if (bounds.width < 1) bounds.width = 1; - if (bounds.height < 1) bounds.height = 1; + if (this.bounds.width < 1) this.bounds.width = 1; + if (this.bounds.height < 1) this.bounds.height = 1; } @Override diff --git a/src/main/java/ovh/gasser/newshapes/shapes/SCircle.java b/src/main/java/ovh/gasser/newshapes/shapes/SCircle.java index 98107de..7921c04 100644 --- a/src/main/java/ovh/gasser/newshapes/shapes/SCircle.java +++ b/src/main/java/ovh/gasser/newshapes/shapes/SCircle.java @@ -22,28 +22,24 @@ public class SCircle extends AbstractShape { @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; - } - } - + 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; - - this.radius = Math.max(newWidth, newHeight) / 2; - bounds.setSize(this.radius * 2, this.radius * 2); + + bounds.setSize(newWidth, newHeight); + this.radius = Math.max(bounds.width, bounds.height) / 2; } @Override diff --git a/src/main/java/ovh/gasser/newshapes/shapes/SText.java b/src/main/java/ovh/gasser/newshapes/shapes/SText.java index bdcadff..d7b7007 100644 --- a/src/main/java/ovh/gasser/newshapes/shapes/SText.java +++ b/src/main/java/ovh/gasser/newshapes/shapes/SText.java @@ -18,7 +18,8 @@ public class SText extends AbstractShape { private final int fontStyle; private SText(int x, int y, String text, int fontSize, String fontName, int fontStyle) { - super(new Rectangle(x, y, 0, 0)); + // Initialize with a reasonable default width/height so hit testing works reliably + super(new Rectangle(x, y, 100, 20)); this.text = normalizeText(text); this.fontSize = fontSize; this.fontName = fontName; diff --git a/src/main/java/ovh/gasser/newshapes/shapes/STriangle.java b/src/main/java/ovh/gasser/newshapes/shapes/STriangle.java index e4c581a..064a253 100644 --- a/src/main/java/ovh/gasser/newshapes/shapes/STriangle.java +++ b/src/main/java/ovh/gasser/newshapes/shapes/STriangle.java @@ -18,21 +18,22 @@ public class STriangle extends AbstractShape { @Override public void resize(ResizeHandle handle, int dx, int dy) { - Rectangle bounds = getBounds(); int delta = Math.max(Math.abs(dx), Math.abs(dy)); boolean shrink = switch (handle) { + case E -> dx < 0; + case W -> dx > 0; + case N -> dy > 0; + case S -> dy < 0; case SE -> (dx < 0 || dy < 0); case NW -> (dx > 0 || dy > 0); - case NE -> (dx < 0); - case SW -> (dx > 0); - case E, W -> (dx < 0); - case N, S -> (dy < 0); + case NE -> (dx < 0 || dy > 0); + case SW -> (dx > 0 || dy < 0); default -> false; }; int sizeChange = shrink ? -delta : delta; - + switch (handle) { case SE, E, W -> { bounds.width += sizeChange;