改变JavaFX中曲线的笔划宽度

时间:2014-06-25 22:59:24

标签: javafx-8 curve variations thickness

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

1 个答案:

答案 0 :(得分:1)

您无法改变笔画宽度。

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

path

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