Graph Workbench SPI :: Parameters and General Guidelines

Parameters

The result is a nice white square on a black background, but the result isn't particularly interesting. It would be nice to let the end user provide some input to make our square a little less mundane.

This is where parameters are required. You can specify any number and almost any type of variable to be input by the user and passed to the algorithm, which can then be used by the processing logic. Anything from colors, to the size of the shapes or the level of depth for more complex shapes can be requested from the user. All that is required to do this is to create a setter method in the algorithm for each parameter, which can be one of a large range of types. Here is an example:

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

public class Tutorial_1_2 implements Algorithm {

    Color 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;
    }
}

The user will now have the option of selecting a color. Don't worry about what sort of GUI component to use for inputting this color, the application will figure out the best way to present this to the user. It will then call your setter method with the color selected. In the above example the square will take on whatever color was selected.

You aren't limited to colors either. As mentioned before, almost any type of parameter can be specified and will be presenting using an appropriate input field:

Other types may be introduced in the future. If you specify a parameter that doesn't fit into one of the above categories, a simple text field will be used. The application will then try to construct an instance of a parameter variable using the string from the text field.

Guidelines

Now before we go any further, this would be a good time to lay down some ground rules that should be followed when writing your algorithms.