JavaFX line fill color

非 Y 不嫁゛ 提交于 2019-11-29 17:42:19
José Pereda

If you check this question, you'll see that you can only use setStroke. Also, a possible approach to generate the same styling is proposed by using a linear gradiant.

This will work (adjust stops at your convenience for more or less black width):

Line line = new Line(0,0,100,0);
line.setStrokeWidth(10);
line.setStroke(new LinearGradient(0d, -5d, 0d, 5d, false,
                CycleMethod.NO_CYCLE, new Stop(0,Color.BLACK), 
                                      new Stop(0.199,Color.BLACK),
                                      new Stop(0.2,Color.RED),
                                      new Stop(0.799,Color.RED),
                                      new Stop(0.8,Color.BLACK)));

Note also that since the gradient is not proportional, you need to use rotation to generate not horizontal lines.

The answer of José Pereda is more elegant but I couldn't get the math right to create diagonal lines so as a workaround I simply created two lines, each with a different color:

Line stroke = new Line(0, 0, 100, 100);
Line fill = new Line(0, 0, 100, 100);
stroke.setStrokeWidth(10);
fill.setStrokeWidth(8);
stroke.setStroke(Color.BLACK);
fill.setStroke(Color.RED);
pane.addAll(stroke, fill);

No math required and I can keep on using the setStartX,setStartY, ... methods of the lines although I now have double the amount of lines.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!