JavaFX canvas: drawing dashed lines

I am using JavaFX GraphicsContext to display the mode on Canvas immediately .

Can I draw dotted lines?

Thank!

+3
source share
3 answers
GC.setLineWidth(2);
GC.setStroke(LASSO_COLOR);
GC.beginPath();
hdashed(x0, x1, y0);
hdashed(x0, x1, y1);
vdashed(x0, y0, y1);
vdashed(x1, y0, y1);
GC.closePath();
GC.stroke();

private void hdashed(double x0, double x1, double yy)
{
    boolean on = true;
    GC.moveTo(x0, yy);
    for (double xx=x0; xx<=x1; xx+=DASH_LENGTH) {
        if (on) GC.lineTo(xx, yy);
        else GC.moveTo(xx, yy);
        on = !on;
    }
}

private void vdashed(double xx, double y0, double y1)
{
    boolean on = true;
    GC.moveTo(xx, y0);
    for (double yy=y0; yy<=y1; yy+=DASH_LENGTH) {
        if (on) GC.lineTo(xx, yy);
        else GC.moveTo(xx, yy);
        on = !on;
    }
}
+2
source

This feature has been added to the JFX 8u40. See the API for more details .

+1
source

There is a setLineDashes method for the dashed line, and everything is still:

...
gc.setStroke(Color.RED);
gc.setLineWidth(1);
gc.setLineDashes(2);
gc.strokeLine(x1, y1, x1, y1);
+1
source

All Articles