1

有没有办法在 JavaFX 中创建具有不同笔画宽度的曲线?例如,从 10 点开始,到 1 点结束。

4

1 回答 1

1

你不能改变笔画宽度。

不过,您可以通过创建一条路径来模拟这种行为,该路径将多条曲线分层,这些曲线从不同的起始位置开始但会聚到一个点。

小路

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.*;
import javafx.stage.Stage;

import java.util.Random;

public class DividedPath extends Application {

    private static final double WIDTH = 400, HEIGHT = 400;
    private static final Random random = new Random(42);

    @Override public void start(Stage stage) {
        CubicCurve curve = randomCurve(WIDTH, HEIGHT);

        Path path = new Path();
        for (int i = -5; i <= +5; i++) {
            path.getElements().addAll(
                    new MoveTo(curve.getStartX() + i, curve.getStartY()),
                    new CubicCurveTo(
                            curve.getControlX1(),
                            curve.getControlY1(),
                            curve.getControlX2(),
                            curve.getControlY2(),
                            curve.getEndX(),
                            curve.getEndY()
                    )
            );
        }
        path.setStroke(Color.FORESTGREEN);

        Group root = new Group();
        curve.setFill(null);
        root.getChildren().addAll(
                path,
                curve
        );

        Scene scene = new Scene(
                root,
                WIDTH, HEIGHT,
                Color.rgb(35,39,50)
        );

        stage.setScene(scene);
        stage.setResizable(false);
        stage.show();
    }

    private CubicCurve randomCurve(double mx, double my) {
        return new CubicCurve(
                random.nextDouble() * mx,
                random.nextDouble() * my,
                random.nextDouble() * mx,
                random.nextDouble() * my,
                random.nextDouble() * mx,
                random.nextDouble() * my,
                random.nextDouble() * mx,
                random.nextDouble() * my
        );
    }
}
于 2014-06-26T08:47:29.417 回答