Thursday, May 15, 2014

JavaFX example: Rectangle filled with LinearGradient


package javafx8_shape;

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.Rectangle;
import javafx.stage.Stage;

/**
 *
 * @web java-buddy.blogspot.com
 */
public class JavaFX8_Shape extends Application {

    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        Scene scene = new Scene(root, 500, 500, Color.BLACK);
        
        Stop[] stops = new Stop[] { 
            new Stop(0, Color.BLACK), 
            new Stop(1, Color.RED)};
        LinearGradient linearGradient = 
                new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stops);

        //LinearGradient rectangle with Stroke
        Rectangle rect1 = new Rectangle(60, 60, 200, 200);
        rect1.setFill(linearGradient);
        rect1.setStroke(Color.RED);
        rect1.setStrokeWidth(10);
        
        Rectangle rect2 = new Rectangle(160, 160, 300, 300);
        rect2.setFill(linearGradient);
        rect2.setStroke(Color.BLUE);
        rect2.setStrokeWidth(10);
        
        root.getChildren().addAll(rect1, rect2);

        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

}

No comments:

Post a Comment