I need to use path gradients (vary the stroke color along a path), but currently couldn't find a way to do it with the current JavaFX API. Note that this is different than applying a linear gradient to a path element. This may seem to work for straight line segments, but fails in some arc configurations and multiple connected path elements.
Would someone offer any suggestions for an approach to this problem?
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.