JavaFX行填充颜色 [英] JavaFX line fill color
问题描述
我正在尝试创建一条具有不同填充和描边颜色的线,如下所示:
I'm trying to create a line with a different fill and stroke color, something like this:
我尝试了以下操作:
Line line = new Line(0,0,100,100);
line.setFill(Color.RED);
line.setStroke(Color.BLACK);
line.setStrokeWidth(10);
但这给了我一条黑线.
but this gives me just a black line.
我想用一条简单的Line做什么还是我必须使用另一个Shape
? (我宁愿使用一行,因为我必须经常调用setStartX
,setStartY
,...方法)
Is what I'm trying to do possible with a simple Line or do I have to use another Shape
? (I would prefer using a line because I have to frequently call the setStartX
, setStartY
, ... methods)
推荐答案
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.
这篇关于JavaFX行填充颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!