Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX Canvas: Drawing dashed lines

Tags:

javafx-2

I'm using JavaFX GraphicsContext for immediate mode drawing on a Canvas.

Is it possible to draw dashed lines?

Thanks!

like image 681
user3313263 Avatar asked Jan 29 '26 11:01

user3313263


2 Answers

There is a method setLineDashes for dashed line and everything is as before:

...
gc.setStroke(Color.RED);
gc.setLineWidth(1);
gc.setLineDashes(2);
gc.strokeLine(x1, y1, x1, y1);
like image 55
Bahriddin Abdiev Avatar answered Feb 01 '26 01:02

Bahriddin Abdiev


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;
    }
}
like image 39
robodanny Avatar answered Feb 01 '26 01:02

robodanny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!