JavaFX LinearGradient create using constructor

Description

JavaFX LinearGradient create using constructor

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.Stop;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;


public class Main extends Application{

    public static void main(String[] args) {
        Application.launch(args);/*  w w w.  j  a v a2  s . c om*/
    }
    
    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 350, 300, Color.WHITE);        Ellipse ellipse = new Ellipse(100, 50 + 70/2, 50, 70/2);
        // Create rectangle
        Rectangle rectangle = new Rectangle();
        rectangle.setX(50);
        rectangle.setY(50);
        rectangle.setWidth(100);
        rectangle.setHeight(70);
        rectangle.setTranslateY(ellipse.prefHeight(-1) + ellipse.getLayoutY() + 10);

        // Create linear gradient
        LinearGradient linearGrad = new LinearGradient(50,     //startX
                                                       50,     //startY
                                                       50,     //endX
                                                       50 + rectangle.prefHeight(-1) + 25,     //endY
                                                       false,  //proportional
                                                       CycleMethod.NO_CYCLE,
                                                       new Stop(0.1f, Color.rgb(255, 200, 0, .784)),
                                                        new Stop(1.0f, Color.rgb(0, 0, 0, .784)));

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


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



PreviousNext

Related