JavaFX line fill color

I am trying to create a line with a different fill and stroke color, something like this:

enter image description here

I tried the following:

Line line = new Line(0,0,100,100);
line.setFill(Color.RED);
line.setStroke(Color.BLACK);
line.setStrokeWidth(10);

but it gives me only a black line.

Am I trying to do this with a simple string or do I need to use another Shape? (I would prefer to use the line, because I often have to call methods setStartX, setStartY...)

+1
source share
2 answers

If you check this question , you will see that you can use only setStroke. In addition, a possible approach to creating the same style is proposed using a linear gradient.

( ):

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)));

line gradient

, , , .

+4

, , , :

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);

, setStartX, setStartY,... , .

0

All Articles