1
API
Thibaud edited this page 2026-03-27 15:43:53 +01:00

API Reference

This document covers the public API for the Java shape editor library (ovh.gasser.newshapes).


Table of Contents


Shape (interface)

Package: ovh.gasser.newshapes.shapes

The root interface for all drawable shapes. Every shape in the model implements this interface.

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();
    Shape clone();
}

Method Descriptions

Method Description
accept(ShapeVisitor visitor) Accepts a visitor, enabling rendering, export, or other operations via the Visitor pattern.
translate(int dx, int dy) Moves the shape by the given delta along each axis.
resize(ResizeHandle handle, int dx, int dy) Resizes the shape by dragging the specified handle by the given delta.
getAttributes(String key) Returns the Attributes instance stored under the given key, or null if absent.
addAttributes(Attributes attr) Adds or replaces the attribute entry keyed by attr.getID().
getBounds() Returns a defensive copy of the shape's bounding rectangle.
clone() Returns a deep copy of the shape.

AbstractShape (abstract class)

Package: ovh.gasser.newshapes.shapes

Base implementation of Shape. Provides shared attribute storage and default implementations of translate, resize, and getBounds. Subclasses must implement accept and clone.

public abstract class AbstractShape implements Shape {
    protected final Rectangle bounds;
    // Attributes stored in a TreeMap<String, Attributes>

    @Override
    public void translate(int dx, int dy);

    @Override
    public void resize(ResizeHandle handle, int dx, int dy);

    @Override
    public Rectangle getBounds();

    @Override
    public abstract Shape clone();
}

Implementation Notes

  • translate() — Delegates directly to bounds.translate(dx, dy).
  • resize() — Switches on the ResizeHandle enum to adjust the bounds x, y, width, or height fields accordingly. Enforces a minimum value of 1 for both width and height.
  • getBounds() — Returns new Rectangle(this.bounds) to prevent external mutation of internal state.
  • clone() — Abstract; each concrete subclass is responsible for providing a correct deep copy.

Concrete Shapes

SRectangle

Package: ovh.gasser.newshapes.shapes

Represents a rectangular shape. Extends AbstractShape.

public class SRectangle extends AbstractShape {

    public static SRectangle create(int x, int y, int width, int height);

    public static SRectangle create(int x, int y, int width, int height, Color color);
}
Factory Method Description
create(x, y, width, height) Creates a rectangle with no fill color.
create(x, y, width, height, color) Creates a rectangle with the given fill color.

SCircle

Package: ovh.gasser.newshapes.shapes

Represents a circular shape. Extends AbstractShape and overrides resize and getBounds to maintain circular geometry.

public class SCircle extends AbstractShape {

    public static SCircle create(int x, int y, int radius);

    public static SCircle create(int x, int y, int radius, Color fillColor);

    @Override
    public void resize(ResizeHandle handle, int dx, int dy);

    @Override
    public Rectangle getBounds();
}
Factory Method Description
create(x, y, radius) Creates a circle with no fill color.
create(x, y, radius, fillColor) Creates a circle with the given fill color.

Override Notes

  • resize() — Adjusts the radius based on the drag delta while keeping the shape circular.
  • getBounds() — Computes the bounding rectangle from the stored center point and radius rather than a raw Rectangle.

STriangle

Package: ovh.gasser.newshapes.shapes

Represents an equilateral triangle. Extends AbstractShape and overrides resize to preserve equilateral proportions.

public class STriangle extends AbstractShape {

    public static STriangle create(int x, int y, int size, Color fillColor, Color strokeColor);

    @Override
    public void resize(ResizeHandle handle, int dx, int dy);
}
Factory Method Description
create(x, y, size, fillColor, strokeColor) Creates an equilateral triangle with explicit fill and stroke colors.

Override Notes

  • resize() — Ensures that resizing keeps all sides equal, preserving the equilateral property.

SText

Package: ovh.gasser.newshapes.shapes

Represents an editable text label. Extends AbstractShape. When the stored text is null or empty, the shape renders a "Click to edit" placeholder.

public class SText extends AbstractShape {

    public static SText create(int x, int y, String text);

    public static SText create(int x, int y, String text, String fontName, int fontStyle, int fontSize);
}
Factory Method Description
create(x, y, text) Creates a text shape using the default font: SansSerif, PLAIN, size 16.
create(x, y, text, fontName, fontStyle, fontSize) Creates a text shape with a fully specified font.

SCollection

Package: ovh.gasser.newshapes.shapes

A composite shape that holds an ordered collection of child Shape instances. Implements both Streamable<Shape> and Iterable<Shape>. Overrides core Shape methods to propagate operations to all children.

public class SCollection extends AbstractShape
        implements Streamable<Shape>, Iterable<Shape> {

    public static SCollection of(Shape... shapes);

    public void add(Shape s);

    public void remove(Shape s);

    @Override
    public Rectangle getBounds();

    @Override
    public Shape clone();

    @Override
    public void translate(int dx, int dy);

    @Override
    public void addAttributes(Attributes attr);
}
Member Description
of(Shape... shapes) Factory method. Creates a collection from the given shapes and automatically adds a SelectionAttributes instance.
add(Shape s) Adds a child shape to the collection.
remove(Shape s) Removes a child shape from the collection.
getBounds() Returns the union of all children's bounding rectangles. Returns the window size if the collection is empty.
clone() Deep-copies the collection by recursively cloning each child.
translate() Propagates the translation to every child shape.
addAttributes() Propagates ColorAttributes to all children; other attribute types are stored locally.

ShapeVisitor (interface)

Package: ovh.gasser.newshapes

Visitor interface for operations over the shape hierarchy (rendering, export, hit-testing, etc.). Each concrete shape calls the appropriate visit method from its accept implementation.

public interface ShapeVisitor {
    void visitRectangle(SRectangle sRectangle);
    void visitCollection(SCollection collection);
    void visitCircle(SCircle sCircle);
    void visitTriangle(STriangle sTriangle);
    void visitText(SText sText);
}

Attributes (interface)

Package: ovh.gasser.newshapes.attributes

Marker interface for metadata that can be attached to any Shape. All attribute types must provide a unique string key via getID().

public interface Attributes {
    String getID();
}

ColorAttributes

Package: ovh.gasser.newshapes.attributes

Stores fill and stroke color information for a shape.

public class ColorAttributes implements Attributes {

    public static final String ID = "COLOR_ATTRS";

    public boolean filled;
    public boolean stroked;
    public Color filledColor;
    public Color strokedColor;

    public ColorAttributes(boolean filled, boolean stroked,
                           Color filledColor, Color strokedColor);

    @Override
    public String getID();  // returns "COLOR_ATTRS"
}
Field Description
filled Whether the shape interior is filled.
stroked Whether the shape outline is drawn.
filledColor The fill color, used when filled is true.
strokedColor The stroke color, used when stroked is true.

SelectionAttributes

Package: ovh.gasser.newshapes.attributes

Tracks whether a shape is currently selected in the editor.

public class SelectionAttributes implements Attributes {

    public static final String ID = "SELECTION_ATTRS";

    public boolean selected;

    public SelectionAttributes();

    public SelectionAttributes(boolean selected);

    @Override
    public String getID();  // returns "SELECTION_ATTRS"
}
Constructor Description
SelectionAttributes() Creates an instance with selected defaulting to false.
SelectionAttributes(boolean selected) Creates an instance with an explicit initial selection state.

Selection (class)

Package: ovh.gasser.newshapes

Maintains the current set of selected shapes. Notifies registered SelectionListener instances when the selection changes. Implements Streamable<Shape>.

public class Selection implements Streamable<Shape> {

    public void add(Shape s);

    public void addAll(Collection<Shape> shapes);

    public void clear();

    public boolean isEmpty();

    public List<Shape> getSelectedShapes();

    public void addListener(SelectionListener listener);
}
Method Description
add(Shape s) Adds a shape to the selection, marks it as selected via its SelectionAttributes, and notifies all listeners.
addAll(Collection<Shape> shapes) Adds multiple shapes to the selection.
clear() Deselects all shapes (updates their SelectionAttributes) and notifies all listeners.
isEmpty() Returns true if no shapes are currently selected.
getSelectedShapes() Returns an unmodifiable copy of the current selection list.
addListener(SelectionListener listener) Registers a listener to be notified on selection changes.

ResizeHandle (enum)

Package: ovh.gasser.newshapes.shapes

Identifies the eight cardinal and diagonal handles used when resizing a shape. Each constant maps to a standard AWT cursor type.

public enum ResizeHandle {
    NW, N, NE,
    W,      E,
    SW, S, SE;

    public int getCursorType();
}
Value Position AWT Cursor
NW Top-left corner Cursor.NW_RESIZE_CURSOR
N Top edge center Cursor.N_RESIZE_CURSOR
NE Top-right corner Cursor.NE_RESIZE_CURSOR
E Right edge center Cursor.E_RESIZE_CURSOR
SE Bottom-right corner Cursor.SE_RESIZE_CURSOR
S Bottom edge center Cursor.S_RESIZE_CURSOR
SW Bottom-left corner Cursor.SW_RESIZE_CURSOR
W Left edge center Cursor.W_RESIZE_CURSOR

getCursorType() returns the java.awt.Cursor constant integer appropriate for the handle's position, suitable for use with Component.setCursor(Cursor.getPredefinedCursor(...)).


Streamable (interface)

Package: ovh.gasser.newshapes.util

A convenience interface that extends Iterable<T> with a default stream() method, allowing any collection-like type in the model to be used directly in Java stream pipelines.

public interface Streamable<T> extends Iterable<T> {

    default Stream<T> stream() {
        return StreamSupport.stream(spliterator(), false);
    }
}

Implemented by SCollection and Selection.