Base java event POC
This commit is contained in:
51
src/main/java/ovh/gasser/newshapes/shapes/AbstractShape.java
Normal file
51
src/main/java/ovh/gasser/newshapes/shapes/AbstractShape.java
Normal file
@@ -0,0 +1,51 @@
|
||||
package ovh.gasser.newshapes.shapes;
|
||||
|
||||
import ovh.gasser.newshapes.attributes.Attributes;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
public abstract class AbstractShape implements Shape {
|
||||
|
||||
private Map<String, Attributes> attributes = new TreeMap<>();
|
||||
private Rectangle bounds;
|
||||
|
||||
AbstractShape() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
AbstractShape(Rectangle bounds) {
|
||||
this.bounds = bounds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Attributes getAttributes(String key) {
|
||||
return attributes.get(key);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addAttributes(Attributes attrs) {
|
||||
attributes.put(attrs.getID(), attrs);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLoc(Point newLoc) {
|
||||
getBounds().setLocation(newLoc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void translate(int dx, int dy) {
|
||||
getBounds().translate(dx, dy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rectangle getBounds() {
|
||||
return this.bounds;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("x=%d, y=%d, width=%d, height=%d", bounds.x, bounds.y, bounds.width, bounds.height);
|
||||
}
|
||||
}
|
||||
65
src/main/java/ovh/gasser/newshapes/shapes/SCollection.java
Normal file
65
src/main/java/ovh/gasser/newshapes/shapes/SCollection.java
Normal file
@@ -0,0 +1,65 @@
|
||||
package ovh.gasser.newshapes.shapes;
|
||||
|
||||
import ovh.gasser.newshapes.App;
|
||||
import ovh.gasser.newshapes.attributes.SelectionAttributes;
|
||||
import ovh.gasser.newshapes.ShapeVisitor;
|
||||
import ovh.gasser.newshapes.util.Streamable;
|
||||
|
||||
import java.awt.*;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Spliterator;
|
||||
|
||||
public class SCollection extends AbstractShape implements Streamable<Shape> {
|
||||
private final List<Shape> children;
|
||||
|
||||
private SCollection(Shape... shapes) {
|
||||
this.children = List.of(shapes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(ShapeVisitor visitor) {
|
||||
visitor.visitCollection(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rectangle getBounds() {
|
||||
try {
|
||||
Rectangle bounds = children.get(0).getBounds();
|
||||
for (Shape s : children) bounds = bounds.union(s.getBounds());
|
||||
return bounds;
|
||||
} catch (IndexOutOfBoundsException e){
|
||||
// If the SCollection is empty, set the bounds to fill the window
|
||||
return new Rectangle(App.WIN_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLoc(Point newLoc) {
|
||||
final Point loc = getBounds().getLocation();
|
||||
children.forEach(s -> s.translate(newLoc.x - loc.x, newLoc.y - loc.y));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Shape> iterator() {
|
||||
return children.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Spliterator<Shape> spliterator() {
|
||||
return children.spliterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("SCollection{");
|
||||
children.forEach(obj -> sb.append(obj).append(", "));
|
||||
return sb.append("}").toString();
|
||||
}
|
||||
|
||||
public static SCollection of(Shape ...shapes) {
|
||||
SCollection collection = new SCollection(shapes);
|
||||
collection.addAttributes(new SelectionAttributes());
|
||||
return collection;
|
||||
}
|
||||
}
|
||||
38
src/main/java/ovh/gasser/newshapes/shapes/SRectangle.java
Normal file
38
src/main/java/ovh/gasser/newshapes/shapes/SRectangle.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package ovh.gasser.newshapes.shapes;
|
||||
|
||||
import ovh.gasser.newshapes.attributes.ColorAttributes;
|
||||
import ovh.gasser.newshapes.attributes.SelectionAttributes;
|
||||
import ovh.gasser.newshapes.ShapeVisitor;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public class SRectangle extends AbstractShape {
|
||||
|
||||
private SRectangle(Rectangle rect) {
|
||||
super(rect);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(ShapeVisitor visitor) {
|
||||
visitor.visitRectangle(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SRectangle{" +
|
||||
super.toString() +
|
||||
'}';
|
||||
}
|
||||
|
||||
public static SRectangle create(int x, int y, int width, int height) {
|
||||
SRectangle rect = new SRectangle(new Rectangle(x, y, width, height));
|
||||
rect.addAttributes(new SelectionAttributes());
|
||||
return rect;
|
||||
}
|
||||
|
||||
public static SRectangle create(int x, int y, int width, int height, Color color) {
|
||||
final SRectangle rect = create(x, y, width, height);
|
||||
rect.addAttributes(new ColorAttributes(false, true, Color.BLACK, color));
|
||||
return rect;
|
||||
}
|
||||
}
|
||||
15
src/main/java/ovh/gasser/newshapes/shapes/Shape.java
Normal file
15
src/main/java/ovh/gasser/newshapes/shapes/Shape.java
Normal file
@@ -0,0 +1,15 @@
|
||||
package ovh.gasser.newshapes.shapes;
|
||||
|
||||
import ovh.gasser.newshapes.ShapeVisitor;
|
||||
import ovh.gasser.newshapes.attributes.Attributes;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
public interface Shape {
|
||||
void accept(ShapeVisitor visitor);
|
||||
void setLoc(Point newLoc);
|
||||
void translate(int dx, int dy);
|
||||
Attributes getAttributes(String key);
|
||||
void addAttributes(Attributes attr);
|
||||
Rectangle getBounds();
|
||||
}
|
||||
Reference in New Issue
Block a user