protected void initExample() {
_canvas.setTitle("Lines");
_lightState.setEnabled(false);
// Create a line with our example "makeLine" method. See method below.
final Line regular = makeLine(new Grapher() {
@Override
double getYforX(final double x) {
// Line eq will be y = x^3 - 2x^2
return Math.pow(x, 3) - (2 * Math.pow(x, 2));
}
}, -5, 5, .25);
// Set some properties on this line.
// Set our line width - for lines this in in screen space... not world space.
regular.setLineWidth(3.25f);
// This line will be green
regular.setDefaultColor(ColorRGBA.GREEN);
// Add our line to the scene.
_root.attachChild(regular);
// Create a line with our example "makeLine" method. See method below.
final Line antialiased = makeLine(new Grapher() {
@Override
double getYforX(final double x) {
// Line eq will be y = -x^3 - 2(-x^2)
return Math.pow(-x, 3) - (2 * Math.pow(-x, 2));
}
}, -5, 5, .25);
// Set some properties on this line.
// Set our line width - for lines this in in screen space... not world space.
antialiased.setLineWidth(3.25f);
// This line will be Cyan.
antialiased.setDefaultColor(ColorRGBA.CYAN);
// Finally let us make this antialiased... see also BlendState below.
antialiased.setAntialiased(true);
// Uncomment to see a dashed line. :)
// antialiased.setStipplePattern((short) 0xFFF0);
// Antialiased lines work by adding small pixels to the line with alpha blending values.
// To make use of this, you need to add a blend state that blends the source color
// with the destination color using the source alpha.
final BlendState blend = new BlendState();
blend.setBlendEnabled(true);
// use source color * source alpha + (1-source color) * destination color.
// (Note: for an interesting effect, switch them so it is OneMinusSourceAlpha/SourceAlpha.
// This will show you the pixels that are being added to your line in antialiasing.)
blend.setSourceFunction(SourceFunction.SourceAlpha);
blend.setDestinationFunction(DestinationFunction.OneMinusSourceAlpha);
antialiased.setRenderState(blend);
// Add our antialiased line to the scene.
_root.attachChild(antialiased);
}