Graph Workbench SPI :: Annotations and Bounds

Annotations

Apart from writing more complex algorithms, one thing you may want to do is customize the way your algorithm appears in the registry, and how its parameters appear in the parameter window. This can be done by using a number of annotations in your source code. An example illustrates this best.

import com.stimware.graphworkbench.spi.Algorithm;
import com.stimware.graphworkbench.spi.Description;
import com.stimware.graphworkbench.spi.Parameter;
import java.awt.Color;

@Description(name="Tutorial 1-3")
public class Tutorial_1_3 implements Algorithm {

    Color color;

    @Parameter(defaultValue="red", label="Square Color")
    public void setColor(Color color) {
        this.color = color;
    }

    public Color calculate(double x, double y) {

        // Is this point within the square?
        if (x > -0.5 && x < 0.5) {
            if (y > -0.5 && y < 0.5) {
                return color;
            }
        }

        return Color.BLACK;
    }
}

This is no different to the previous example, except for the following cosmetic changes:

These annotations are purely optional but make using your algorithm a more pleasant experience.

Bounds

You are most certainly aware that it is possible to zoom in on areas of the graph and render specific portions. It is also possible to specify a region to render by default, rather than the square bound by the coordinates (-1,-1) and (1,1). This requires the DefaultBounds annotation.

import com.stimware.graphworkbench.spi.Algorithm;
import com.stimware.graphworkbench.spi.DefaultBounds;
import com.stimware.graphworkbench.spi.Description;
import com.stimware.graphworkbench.spi.Parameter;
import java.awt.Color;

@Description(name="Tutorial 1-4")
@DefaultBounds(top=2, right=2, bottom=-2, left=-2)
public class Tutorial_1_4 implements Algorithm {

    Color color;

    @Parameter(defaultValue="red", label="Square Color")
    public void setColor(Color color) {
        this.color = color;
    }

    public Color calculate(double x, double y) {

        // Is this point within the square?
        if (x > -0.5 && x < 0.5) {
            if (y > -0.5 && y < 0.5) {
                return color;
            }
        }

        return Color.BLACK;
    }
}

This one is actually zoomed out a bit. Sometimes your algorithm only displays interesting detail at certain graph coordinate, so you can either scale (or normalize if you will) the algorithm or get it to render just the interesting bits by default. The user can still of course enter their own coordinates of interest.