【问题标题】:Rounded corners in javafxjavafx中的圆角
【发布时间】:2018-09-23 19:27:53
【问题描述】:

有什么方法可以在 javafx 中使折线的角变圆。我尝试了笔划线连接,但它没有用。我想像下图中的细线一样制作角落。

【问题讨论】:

  • 你有没有为此尝试过?
  • 图像看起来不像一个单一的形状,更不用说Polyline。你指的是图片的哪一部分?
  • 我的意思是细线。我尝试了我在问题中所说的笔画线 jon 方法,但它没有用。我也尝试过使用 Path 和 ArcTo,但我也做不到。

标签: javafx rounded-corners polyline


【解决方案1】:

线连接在这里没有帮助,因为它们控制了在线连接点处方向不同的 2 个路径元素的行为。他们不修改路径元素本身。您需要改用Path

ArcToQuadCurveTo 为您提供创建圆角的选项。两者看起来非常相似。下面的代码让你玩弄距离曲线开始的拐角点的距离:

private Path arcPath(DoubleProperty distanceProperty) {
    MoveTo moveTo = new MoveTo(300, 300);

    // end the line at the given distance right of the intersection of the lines
    HLineTo lineTo1 = new HLineTo();
    lineTo1.xProperty().bind(distanceProperty.add(50));

    ArcTo arcTo = new ArcTo();

    // end the curve at the given distance above the intersection of the lines
    arcTo.setX(50);
    arcTo.yProperty().bind(distanceProperty.negate().add(300));

    // radius is equal to the distance
    arcTo.radiusXProperty().bind(distanceProperty);
    arcTo.radiusYProperty().bind(distanceProperty);

    arcTo.setSweepFlag(true);

    VLineTo lineTo2 = new VLineTo(50);
    return new Path(moveTo, lineTo1, arcTo, lineTo2);
}

private Path quadPath(DoubleProperty distanceProperty) {
    MoveTo moveTo = new MoveTo(300, 300);

    // end the line at the given distance right of the intersection of the lines
    HLineTo lineTo1 = new HLineTo();
    lineTo1.xProperty().bind(distanceProperty.add(50));

    QuadCurveTo curveTo = new QuadCurveTo();

    // control point at the location where the lines would intersect
    curveTo.setControlX(50);
    curveTo.setControlY(300);

    // end the curve at the given distance above the intersection of the lines
    curveTo.setX(50);
    curveTo.yProperty().bind(distanceProperty.negate().add(300));

    VLineTo lineTo2 = new VLineTo(50);
    return new Path(moveTo, lineTo1, curveTo, lineTo2);
}
Slider distanceSlider = new Slider(0, 250, 10);
Label label = new Label();
label.textProperty().bind(distanceSlider.valueProperty().asString("%.2f"));
HBox controls = new HBox(distanceSlider, label);

Path path1 = quadPath(distanceSlider.valueProperty());
Path path2 = arcPath(distanceSlider.valueProperty());

VBox root = new VBox(new HBox(new Pane(path1), new Pane(path2)), controls);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-21
    • 2018-05-07
    • 2013-05-10
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    相关资源
    最近更新 更多