Is there a way to build a gradient path in JavaFX?

I need to use path gradients (change the color of the stroke along the path), but there is currently no way to find it using the current JavaFX API. Note that this is different from applying a linear gradient to a path element. This may work for straight line segments, but it does not work in some arc configurations and several related path elements.

Anyone offer any suggestions on how to approach this problem?

+6
source share
1 answer

You can try the following approach:

@Override public void start(Stage primaryStage) { Group root = new Group(); // CREATE CANVAS final Canvas canvas = new Canvas(300, 250); // GET GRAPHICS CONTEXT final GraphicsContext gc = canvas.getGraphicsContext2D(); // DRAW THE SHAPE (LINE) gc.beginPath(); gc.moveTo(50, 50); //Begin gc.lineTo(150, 200); //End gc.closePath(); // CREATE THE LINEAR EFFECT LinearGradient lg = new LinearGradient(0, 0, 1, 1, true, CycleMethod.REFLECT, new Stop(0.0, Color.RED), new Stop(0.5, Color.GREEN), new Stop(1.0, Color.BLUE)); // SET & STROKE WITH LINEAR gc.setLineWidth(20); gc.setStroke(lg); gc.stroke(); //ADD CANVAS NODE TO ROOT root.getChildren().add(canvas); primaryStage.setScene(new Scene(root)); primaryStage.show(); } 
0
source

Source: https://habr.com/ru/post/969342/


All Articles