JavaFX 颜色线性渐变

照例,上图,上代码,解释都在注释里。 

JavaFX 颜色线性渐变_第1张图片

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.layout.FlowPane;
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 LinearGradientTest extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        // 一个方向渐变
        Rectangle rectangle1 = new Rectangle();
        rectangle1.setArcHeight(4);
        rectangle1.setArcWidth(4);
        rectangle1.setStroke(Color.rgb(204,204,204));
        rectangle1.setStrokeWidth(1);
        rectangle1.setWidth(184);
        rectangle1.setHeight(28);
        // startX,startY定线性渐变起点;endX,endY定线性渐变终点;百分比形式,0至1;起点和终点可以定线性渐变的方向
        // 仿JavaFX默认按钮风格
        /*LinearGradient linearGradient1 = new LinearGradient(0, 0, 0, 1, true, CycleMethod.NO_CYCLE, new Stop[] {
                new Stop(0, Color.rgb(242, 242, 242)),
                new Stop(1, Color.rgb(229, 229, 229))
        });*/
        
        LinearGradient linearGradient1 = new LinearGradient(0, 0, 0, 1, true, CycleMethod.NO_CYCLE, new Stop[] {
                new Stop(0, javafx.scene.paint.Color.rgb(135,206,250)),
                new Stop(1, Color.rgb(0,0,255))
        });
        rectangle1.setFill(linearGradient1);

        // 倒影渐变
        Rectangle rectangle2 = new Rectangle();
        rectangle2.setArcHeight(4);
        rectangle2.setArcWidth(4);
        rectangle2.setStroke(Color.rgb(204,204,204));
        rectangle2.setStrokeWidth(1);
        rectangle2.setWidth(184);
        rectangle2.setHeight(28);

        // CycleMethod.NO_CYCLE为倒影模式
        LinearGradient linearGradient2 = new LinearGradient(0, 0, 0.5, 0, true, CycleMethod.REFLECT, new Stop[] {
                new Stop(0, Color.rgb(135,206,250)),
                new Stop(1, Color.rgb(0,0,255)),
                new Stop(0, Color.rgb(135,206,250))
        });
        rectangle2.setFill(linearGradient2);

        // 重复渐变
        Rectangle rectangle3 = new Rectangle();
        rectangle3.setArcHeight(4);
        rectangle3.setArcWidth(4);
        rectangle3.setStroke(javafx.scene.paint.Color.rgb(204,204,204));
        rectangle3.setStrokeWidth(1);
        rectangle3.setWidth(184);
        rectangle3.setHeight(28);

        // CycleMethod.REPEAT为重复模式
        LinearGradient linearGradient3 = new LinearGradient(0, 0, 0.25, 0, true, CycleMethod.REPEAT, new Stop[] {
                new Stop(0, Color.rgb(135,206,250)),
                new Stop(1, Color.rgb(0,0,255)),
        });
        rectangle3.setFill(linearGradient3);

        FlowPane flowPane = new FlowPane(rectangle1, rectangle2, rectangle3);
        flowPane.setAlignment(Pos.CENTER);
        flowPane.setOrientation(Orientation.HORIZONTAL);
        flowPane.setPrefHeight(200);
        flowPane.setPrefWidth(200);
        flowPane.setVgap(20);

        primaryStage.setScene(new Scene(flowPane));
        primaryStage.show();
    }
}

 

你可能感兴趣的:(Java,#,JavaFX)