Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFx 2.x : How to draw dashed or dotted lines?

Tags:

I would like to dynamically change the draw of a line from solid, dotted or dashed: it seems I have to use line.setStroke, is it the correct method?

And, how to accomplish this?

Thanks

like image 780
Alberto acepsut Avatar asked Oct 08 '12 17:10

Alberto acepsut


People also ask

How do you make a dotted line in Javafx?

I have found a solution:by using this method line. setStyle("-fx-stroke-dash-array: 2 12 12 2;"); I must using a refresh such as pane. getChildren(). remove(line); pane.

What is dashed or dotted lines?

"Dashed line" means a line made up of short strokes with breaks in between. You would put the pencil down, draw a short distance, pick it up and move it just a little, then put it down and draw another short stroke, etc.


1 Answers

No that is not the correct method, setStroke sets the color of the stroke.
Correct method is getStrokeDashArray().add():

Line line1 = new Line(20, 40, 270, 40);
line1.getStrokeDashArray().addAll(25d, 20d, 5d, 20d);

Line line2 = new Line(20, 60, 270, 60);
line2.getStrokeDashArray().addAll(50d, 40d);

Line line3 = new Line(20, 80, 270, 80);
line3.getStrokeDashArray().addAll(25d, 10d);

Line line4 = new Line(20, 100, 270, 100);
line4.getStrokeDashArray().addAll(2d);

Line line5 = new Line(20, 120, 270, 120);
line5.getStrokeDashArray().addAll(2d, 21d);

pane.getChildren().addAll(line1, line2, line3, line4, line5);

StrokeDashArray defines the pattern of line and gap sequences. See the following different patterns as output of aboves:

enter image description here

Of course by manipulating the StrokeDashArray array elements you can change the pattern dynamically.

like image 112
Uluk Biy Avatar answered Nov 18 '22 17:11

Uluk Biy