JavaFX 漸變顏色

2018-03-06 18:49 更新

JavaFX教程 - JavaFX漸變顏色


我們可以使用徑向漸變使形狀看起來三維。

梯度繪制可以在兩種或更多種顏色之間內插,這給出形狀的深度。

JavaFX提供兩種類型的漸變:徑向漸變( RadialGradient )和線性漸變( LinearGradient )。

要在JavaFX中創(chuàng)建漸變顏色,我們需要設置五個屬性。

  • 設置開始第一個停止顏色的起點。
  • 將終點設置為終止停止顏色。
  • 設置proportional屬性以指定是使用標準屏幕坐標還是單位平方坐標。
  • 將循環(huán)方法設置為使用三個枚舉:NO_CYCLE,REFLECT或REPEAT。
  • 設置Stop顏色數(shù)組。

通過將proportional屬性設置為false,我們可以基于標準屏幕(x,y)坐標將漸變軸設置為具有起點和終點。

通過將proportional屬性設置為true,梯度軸線開始點和結束點將被表示為單位平方坐標。起點和終點的x,y坐標必須在0.0和1.0之間(double)。


線性梯度

要創(chuàng)建線性漸變繪制,我們?yōu)殚_始點和結束點指定startX,startY,endX和endY。起點和終點坐標指定漸變圖案開始和停止的位置。

下表列出了LinearGradient屬性

屬性數(shù)據類型 / 說明
startXDouble
設置梯度軸起點的X坐標。
startYDouble
設置梯度軸起點的Y坐標。
endXDouble
設置梯度軸終點的X坐標。
endYDouble
設置梯度軸終點的Y坐標。
proportionalBoolean
設置坐標是否與形狀成比例。 true將使用單位正方形坐標,否則使用屏幕坐標系。
cycleMethodCycleMethod
設置應用于漸變的循環(huán)方法。
stopsList<Stop>
置梯度顏色規(guī)范的停止列表。

以下代碼顯示了如何使用線性漸變繪制矩形。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
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 Main extends Application {

    @Override
    public void start(Stage stage) {
        VBox box = new VBox();
        final Scene scene = new Scene(box,300, 250);
        scene.setFill(null);
        Stop[] stops = new Stop[] { new Stop(0, Color.BLACK), new Stop(1, Color.RED)};
        LinearGradient lg1 = new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stops);
        
        Rectangle r1 = new Rectangle(0, 0, 100, 100);
        r1.setFill(lg1);
        
        box.getChildren().add(r1);
        
        stage.setScene(scene);
        stage.show();
    }

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

上面的代碼生成以下結果。

null

徑向梯度

下表列出了RadialGradient屬性。

屬性數(shù)據類型 / 說明
focusAngleDouble
設置從漸變中心到映射第一種顏色的焦點的角度(以度為單位)。
focusDistanceDouble
設置從漸變中心到映射第一種顏色的焦點的距離。
centerXDouble
設置漸變圓的中心點的X坐標。
centerYDouble
設置漸變圓的中心點的Y坐標。
radiusDouble
設置顏色漸變的圓的半徑。
proportionalboolean
設置坐標和大小與形狀成比例。
cycleMethodCycleMethod
設置應用于漸變的循環(huán)方法。
StopsList<Stop>
設置漸變顏色的停止列表
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.RadialGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class Main extends Application {
    static int dx = 1;
    static int dy = 1;

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

    @Override
    public void start(final Stage primaryStage) {
        primaryStage.setTitle("Animation");
        Group root = new Group();
        Scene scene = new Scene(root, 400, 300, Color.WHITE);

        primaryStage.setScene(scene);
        addBouncyBall(scene); 
        primaryStage.show();
    }
    private void addBouncyBall(final Scene scene) {
        final Circle ball = new Circle(100, 100, 20);

        RadialGradient gradient1 = new RadialGradient(0,
            .1,
            100,
            100,
            20,
            false,
            CycleMethod.NO_CYCLE,
            new Stop(0, Color.RED),
            new Stop(1, Color.BLACK));

        ball.setFill(gradient1);

    
        final Group root = (Group) scene.getRoot();
        root.getChildren().add(ball);

    }
}

上面的代碼生成以下結果。

null

半透明漸變

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
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 Main extends Application {

    @Override
    public void start(Stage stage) {
        VBox box = new VBox();
        final Scene scene = new Scene(box,300, 250);
        scene.setFill(null);
        // A rectangle filled with a linear gradient with a translucent color.
        Rectangle rectangle = new Rectangle();
        rectangle.setX(50);
        rectangle.setY(50);
        rectangle.setWidth(100);
        rectangle.setHeight(70);

        LinearGradient linearGrad = new LinearGradient(
                0,   // start X 
                0,   // start Y
                0,   // end X
                1, // end Y
                true, // proportional
                CycleMethod.NO_CYCLE, // cycle colors
                // stops
                new Stop(0.1f, Color.rgb(25, 200, 0, .4)),
                new Stop(1.0f, Color.rgb(0, 0, 0, .1)));
        rectangle.setFill(linearGrad);
        
        box.getChildren().add(rectangle);
        
        stage.setScene(scene);
        stage.show();
    }

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

上面的代碼生成以下結果。

null

反射循環(huán)漸變

以下代碼使用在對角線方向上的綠色和黑色創(chuàng)建具有漸變的重復圖案的矩形。

開始X,Y和結束X,Y值設置在對角線位置,循環(huán)方法設置為反映CycleMethod.REFLECT 。

CycleMethod.REFLECT使梯度圖案在停止顏色之間重復或循環(huán)。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
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 Main extends Application {

  @Override
  public void start(Stage stage) {
    VBox box = new VBox();
    final Scene scene = new Scene(box, 300, 250);
    scene.setFill(null);
    // A rectangle filled with a linear gradient with a translucent color.
    Rectangle rectangle = new Rectangle();
    rectangle.setX(50);
    rectangle.setY(50);
    rectangle.setWidth(100);
    rectangle.setHeight(70);

    LinearGradient cycleGrad = new LinearGradient(50, // start X
        50, // start Y
        70, // end X
        70, // end Y
        false, // proportional
        CycleMethod.REFLECT, // cycleMethod
        new Stop(0f, Color.rgb(21, 25, 0, .784)), new Stop(1.0f, Color.rgb(0,
            210, 0, .784)));
    rectangle.setFill(cycleGrad);

    box.getChildren().add(rectangle);

    stage.setScene(scene);
    stage.show();
  }

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

上面的代碼生成以下結果。

null
以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號