implement resize feature
This commit is contained in:
@@ -34,6 +34,45 @@ public abstract class AbstractShape implements Shape {
|
||||
getBounds().translate(dx, dy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resize(ResizeHandle handle, int dx, int dy) {
|
||||
Rectangle bounds = getBounds();
|
||||
switch (handle) {
|
||||
case E -> bounds.width += dx;
|
||||
case W -> {
|
||||
bounds.x += dx;
|
||||
bounds.width -= dx;
|
||||
}
|
||||
case S -> bounds.height += dy;
|
||||
case N -> {
|
||||
bounds.y += dy;
|
||||
bounds.height -= dy;
|
||||
}
|
||||
case SE -> {
|
||||
bounds.width += dx;
|
||||
bounds.height += dy;
|
||||
}
|
||||
case SW -> {
|
||||
bounds.x += dx;
|
||||
bounds.width -= dx;
|
||||
bounds.height += dy;
|
||||
}
|
||||
case NE -> {
|
||||
bounds.width += dx;
|
||||
bounds.y += dy;
|
||||
bounds.height -= dy;
|
||||
}
|
||||
case NW -> {
|
||||
bounds.x += dx;
|
||||
bounds.width -= dx;
|
||||
bounds.y += dy;
|
||||
bounds.height -= dy;
|
||||
}
|
||||
}
|
||||
if (bounds.width < 1) bounds.width = 1;
|
||||
if (bounds.height < 1) bounds.height = 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rectangle getBounds() {
|
||||
return this.bounds;
|
||||
|
||||
24
src/main/java/ovh/gasser/newshapes/shapes/ResizeHandle.java
Normal file
24
src/main/java/ovh/gasser/newshapes/shapes/ResizeHandle.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package ovh.gasser.newshapes.shapes;
|
||||
|
||||
import java.awt.Cursor;
|
||||
|
||||
public enum ResizeHandle {
|
||||
NW(Cursor.NW_RESIZE_CURSOR),
|
||||
N(Cursor.N_RESIZE_CURSOR),
|
||||
NE(Cursor.NE_RESIZE_CURSOR),
|
||||
E(Cursor.E_RESIZE_CURSOR),
|
||||
SE(Cursor.SE_RESIZE_CURSOR),
|
||||
S(Cursor.S_RESIZE_CURSOR),
|
||||
SW(Cursor.SW_RESIZE_CURSOR),
|
||||
W(Cursor.W_RESIZE_CURSOR);
|
||||
|
||||
private final int cursorType;
|
||||
|
||||
ResizeHandle(int cursorType) {
|
||||
this.cursorType = cursorType;
|
||||
}
|
||||
|
||||
public int getCursorType() {
|
||||
return cursorType;
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ import java.awt.*;
|
||||
|
||||
public class SCircle extends AbstractShape {
|
||||
|
||||
private final int radius;
|
||||
private int radius;
|
||||
|
||||
private SCircle(int x, int y, int radius) {
|
||||
super(new Rectangle(x, y, radius * 2, radius * 2));
|
||||
@@ -20,6 +20,32 @@ public class SCircle extends AbstractShape {
|
||||
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);
|
||||
|
||||
@@ -16,6 +16,54 @@ public class STriangle extends AbstractShape {
|
||||
visitor.visitTriangle(this);
|
||||
}
|
||||
|
||||
@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 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);
|
||||
default -> false;
|
||||
};
|
||||
|
||||
int sizeChange = shrink ? -delta : delta;
|
||||
|
||||
switch (handle) {
|
||||
case SE, E, W -> {
|
||||
bounds.width += sizeChange;
|
||||
bounds.height += sizeChange;
|
||||
}
|
||||
case NW -> {
|
||||
bounds.x -= sizeChange;
|
||||
bounds.width += sizeChange;
|
||||
bounds.y -= sizeChange;
|
||||
bounds.height += sizeChange;
|
||||
}
|
||||
case NE -> {
|
||||
bounds.y -= sizeChange;
|
||||
bounds.width += sizeChange;
|
||||
bounds.height += sizeChange;
|
||||
}
|
||||
case SW -> {
|
||||
bounds.x -= sizeChange;
|
||||
bounds.width += sizeChange;
|
||||
bounds.height += sizeChange;
|
||||
}
|
||||
case N, S -> {
|
||||
bounds.width += sizeChange;
|
||||
bounds.height += sizeChange;
|
||||
}
|
||||
}
|
||||
|
||||
if (bounds.width < 1) bounds.width = 1;
|
||||
if (bounds.height < 1) bounds.height = 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Shape clone() {
|
||||
var color = (ColorAttributes) getAttributes(ColorAttributes.ID);
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.awt.*;
|
||||
public interface Shape {
|
||||
void accept(ShapeVisitor visitor);
|
||||
void translate(int dx, int dy);
|
||||
void resize(ResizeHandle handle, int dx, int dy);
|
||||
Attributes getAttributes(String key);
|
||||
void addAttributes(Attributes attr);
|
||||
Rectangle getBounds();
|
||||
|
||||
Reference in New Issue
Block a user