Add LinearGradient to Rectangle : LinearGradient « JavaFX « Java






Add LinearGradient to Rectangle

 
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.LinearGradientBuilder;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.RectangleBuilder;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Colors");
        Group root = new Group();
        Scene scene = new Scene(root, 350, 300, Color.WHITE);
        Rectangle rectangle = RectangleBuilder.create()
            .x(50)
            .y(50)
            .width(100)
            .height(70)
            .translateY(10)
            .build();

        LinearGradient linearGrad = LinearGradientBuilder.create()
            .startX(50)
            .startY(50)
            .endX(50)
            .endY(50 + rectangle.prefHeight(-1) + 25)
            .proportional(false)
            .cycleMethod(CycleMethod.NO_CYCLE)
            .stops( new Stop(0.1f, Color.rgb(255, 200, 0, .784)),
                    new Stop(1.0f, Color.rgb(0, 0, 0, .784)))
            .build();

        rectangle.setFill(linearGrad);
        root.getChildren().add(rectangle); 


  
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

   
  








Related examples in the same category

1.LinearGradient and stop