Fix bugs: stroke condition, logging typo, NPE in clone(), mutable bounds

This commit is contained in:
2026-03-19 22:15:16 +01:00
parent e742bbd827
commit 43769e82ee
6 changed files with 13 additions and 7 deletions

View File

@@ -75,7 +75,7 @@ public abstract class AbstractShape implements Shape {
@Override @Override
public Rectangle getBounds() { public Rectangle getBounds() {
return this.bounds; return new Rectangle(this.bounds);
} }
@Override @Override

View File

@@ -49,7 +49,8 @@ public class SCircle extends AbstractShape {
@Override @Override
public Shape clone() { public Shape clone() {
var color = (ColorAttributes) getAttributes(ColorAttributes.ID); var color = (ColorAttributes) getAttributes(ColorAttributes.ID);
return SCircle.create(super.getBounds().x, super.getBounds().y, this.radius, color.strokedColor); Color strokeColor = color != null ? color.strokedColor : Color.BLACK;
return SCircle.create(super.getBounds().x, super.getBounds().y, this.radius, strokeColor);
} }
public int getRadius() { public int getRadius() {

View File

@@ -39,7 +39,7 @@ public class SCollection extends AbstractShape implements Streamable<Shape> {
for (Shape s : children) bounds = bounds.union(s.getBounds()); for (Shape s : children) bounds = bounds.union(s.getBounds());
return bounds; return bounds;
} catch (IndexOutOfBoundsException e){ } catch (IndexOutOfBoundsException e){
logger.error("getBounds(): {e}", e); logger.error("getBounds(): {}", e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }

View File

@@ -27,7 +27,8 @@ public class SRectangle extends AbstractShape {
@Override @Override
public Shape clone() { public Shape clone() {
var color = (ColorAttributes) this.getAttributes(ColorAttributes.ID); var color = (ColorAttributes) this.getAttributes(ColorAttributes.ID);
return SRectangle.create(super.getBounds().x, super.getBounds().y, getBounds().width, getBounds().height, color.strokedColor); Color strokeColor = color != null ? color.strokedColor : Color.BLACK;
return SRectangle.create(super.getBounds().x, super.getBounds().y, getBounds().width, getBounds().height, strokeColor);
} }
public static SRectangle create(int x, int y, int width, int height) { public static SRectangle create(int x, int y, int width, int height) {

View File

@@ -67,7 +67,9 @@ public class STriangle extends AbstractShape {
@Override @Override
public Shape clone() { public Shape clone() {
var color = (ColorAttributes) getAttributes(ColorAttributes.ID); var color = (ColorAttributes) getAttributes(ColorAttributes.ID);
return STriangle.create(super.getBounds().x, super.getBounds().y, super.getBounds().height, color.strokedColor, color.filledColor); Color strokeColor = color != null ? color.strokedColor : Color.BLACK;
Color fillColor = color != null ? color.filledColor : Color.BLACK;
return STriangle.create(super.getBounds().x, super.getBounds().y, super.getBounds().height, fillColor, strokeColor);
} }
public static STriangle create(int x, int y, int size, Color filledColor, Color strokedColor) { public static STriangle create(int x, int y, int size, Color filledColor, Color strokedColor) {

View File

@@ -60,8 +60,10 @@ public class ShapeDraftman implements ShapeVisitor {
this.g2d.setColor(colAttrs.filledColor); this.g2d.setColor(colAttrs.filledColor);
this.g2d.fillOval(bounds.x, bounds.y, 2 * circle.getRadius(), 2 * circle.getRadius()); this.g2d.fillOval(bounds.x, bounds.y, 2 * circle.getRadius(), 2 * circle.getRadius());
} }
if (colAttrs.stroked) this.g2d.setColor(colAttrs.strokedColor); if (colAttrs.stroked) {
this.g2d.setColor(colAttrs.strokedColor);
this.g2d.drawOval(bounds.x, bounds.y, 2 * circle.getRadius(), 2 * circle.getRadius()); this.g2d.drawOval(bounds.x, bounds.y, 2 * circle.getRadius(), 2 * circle.getRadius());
}
drawHandlerIfSelected(circle); drawHandlerIfSelected(circle);
} }