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:
- String - A text field will be displayed.
- Numeric - A text field will be displayed, and the value entered must be numeric. Integer or decimal types can be used.
- Boolean - A checkbox will be displayed.
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.
- Algorithm instances aren't re-used. Any parameters passed to the algorithm will be reset when a new graph is created, but they will retain their state in between calls to each coordinate.
- A call to your algorithm's calculate method will be made once for each coordinate that corresponds to a pixel. There is no guarantee about the order in which they will be called, so don't write your code to depend on results from previous calculations.
- These calls to calculate may even be performed at the same time by different threads, so make sure your code is threadsafe. Avoid modifying instance or class variables within the calculate method.