Saturday, June 18, 2016

44. RadialGradient

Here, we have 4 circles, with different radial gradient fills.


The focus angle changes from 45 degrees to 315 degrees, in 90 degrees steps. This transverse the quadrants in reverse order. Note if the radius (5th parameter in constructor) is too small, we won't be able to see much difference in the four circles.


package ex44;
 
import java.util.ArrayList;
import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
 
public class Ex44 extends Application {
 
    VBox root;
    ArrayList stop;
    CycleMethod ncyc;
    
    @Override
    public void start(Stage primaryStage) {
 
        stop = new ArrayList<>();
        stop.add(new Stop(0,Color.RED));
        stop.add(new Stop(0.5,Color.GREEN));
        stop.add(new Stop(1,Color.BLUE));
        ncyc = CycleMethod.NO_CYCLE;
        
        root = new VBox(50);
        root.setStyle("-fx-font-size: 16pt;");
        Scene scene = new Scene(root, 400, 600);
        
        root.setAlignment(Pos.CENTER);
        
        createRadialFill(45); // quadrant 4
        createRadialFill(135); // quadrant 3
        createRadialFill(225); // quadrant 2
        createRadialFill(315); // quadrant 1
       
        primaryStage.setTitle("Example 44. Radial Gradient");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
    
    private void createRadialFill(double ang) {
        Circle circle = new Circle(50);
        Label label = new Label(String.valueOf(ang));
        HBox hbox = new HBox(50,label,circle);
        hbox.setAlignment(Pos.CENTER);
        RadialGradient rg = new RadialGradient(
                ang, 0.5, // focus angle, distance
                0.5, 0.5, // center
                0.5, // radius
                true, // proportional
                ncyc, // no-cycle
                stop // colors
        );
        circle.setFill(rg);
        root.getChildren().add(hbox);
    }
}

This is the output:


No comments:

Post a Comment