JavaFX 颜色线性渐变

64 篇文章 12 订阅

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

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();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风铃峰顶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值