Tuesday, June 14, 2016

34. LinearGradient

With LinearGradient, we can have a fill as a continuous mix of colors. This mixture is defined in the Stop array, which is named stop here. Here it goes from Red (0 percent), Green (50 percent), Blue (100 percent). Here, the values are percentage since the flag, for proportional, in all 4 LinearGradient objects, is true.


There are 4 rectangles (all squares), with gradient going in different directions. The directions are based on first 4 numbers: startX, startY, stopX, stopY.


For rectangle 1, this is downwards.


For rectangle 2, this is rightwards.


For rectangle 3, this is diagonal from upper-left to lower-right.


For rectangle 4, this is diagonal from lower-left to upper-right.


package ex34;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
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;
 
public class Ex34 extends Application {
 
    @Override
    public void start(Stage primaryStage) {
 
        GridPane root = new GridPane();
        root.setVgap(100);
        root.setHgap(100);
        root.setAlignment(Pos.CENTER);
        
        Stop[] stop = new Stop[] {
            new Stop(0,Color.RED),
            new Stop(0.5,Color.GREEN),
            new Stop(1,Color.BLUE)
        };
        CycleMethod ncyc = CycleMethod.NO_CYCLE;
        
        Rectangle rect1 = new Rectangle();
        rect1.setWidth(100);
        rect1.setHeight(100);
        rect1.setStroke(Color.RED);
        rect1.setStrokeWidth(5);
        GridPane.setConstraints(rect1, 0, 0);
        LinearGradient lingrad1 = new LinearGradient(
            0.5, 0, 0.5, 1, true, ncyc, stop);
        rect1.setFill(lingrad1);
        
        Rectangle rect2 = new Rectangle();
        rect2.setWidth(100);
        rect2.setHeight(100);
        rect2.setStroke(Color.GREEN);
        rect2.setStrokeWidth(5);
        GridPane.setConstraints(rect2, 1, 0);
        LinearGradient lingrad2 = new LinearGradient(
            0, 0.5, 1, 0.5, true, ncyc, stop);
        rect2.setFill(lingrad2);
        
        Rectangle rect3 = new Rectangle();
        rect3.setWidth(100);
        rect3.setHeight(100);
        rect3.setStroke(Color.BLUE);
        rect3.setStrokeWidth(5);
        GridPane.setConstraints(rect3, 0, 1);
        LinearGradient lingrad3 = new LinearGradient(
            0, 0, 1, 1, true, ncyc, stop);
        rect3.setFill(lingrad3);
        
        Rectangle rect4 = new Rectangle();
        rect4.setWidth(100);
        rect4.setHeight(100);
        rect4.setStroke(Color.YELLOW);
        rect4.setStrokeWidth(5);
        GridPane.setConstraints(rect4, 1, 1);
        LinearGradient lingrad4 = new LinearGradient(
            0, 1, 1, 0, true, ncyc, stop);
        rect4.setFill(lingrad4);
        
        root.getChildren().addAll(rect1, rect2, rect3, rect4);
        Scene scene = new Scene(root, 400, 400);
        
        primaryStage.setTitle("Example 34. Linear Gradient");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}

This is the output:


No comments:

Post a Comment