JavaFX线填充颜色

时间:2015-02-27 11:35:05

标签: line javafx-8

我试图用不同的填充和描边颜色创建一条线,如下所示:

enter image description here

我尝试了以下内容:

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

但这给了我一条黑线。

我尝试使用简单的线路做什么或者是否必须使用其他Shape? (我更喜欢使用一行,因为我必须经常拨打setStartXsetStartY,...方法)

2 个答案:

答案 0 :(得分:4)

如果您选中此question,则会看到您只能使用setStroke。此外,通过使用线性渐变来提出生成相同样式的可能方法。

这样可以使用(在方便时调整停止或多或少的黑色宽度):

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

另请注意,由于渐变不成比例,因此您需要使用旋转来生成非水平线。

答案 1 :(得分:0)

JoséPereda的答案更优雅但是我无法正确地创建对角线,因此我只需创建两条线,每条线都有不同的颜色:

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

不需要数学,我可以继续使用行的setStartXsetStartY,...方法,尽管我现在有两倍的行数。