JavaFX 漸變顏色

2018-03-06 18:49 更新

JavaFX教程 - JavaFX漸變顏色


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

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

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

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

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

通過將proportional屬性設(shè)置為false,我們可以基于標(biāo)準(zhǔn)屏幕(x,y)坐標(biāo)將漸變軸設(shè)置為具有起點(diǎn)和終點(diǎn)。

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


線性梯度

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

下表列出了LinearGradient屬性

屬性數(shù)據(jù)類型 / 說明
startXDouble
設(shè)置梯度軸起點(diǎn)的X坐標(biāo)。
startYDouble
設(shè)置梯度軸起點(diǎn)的Y坐標(biāo)。
endXDouble
設(shè)置梯度軸終點(diǎn)的X坐標(biāo)。
endYDouble
設(shè)置梯度軸終點(diǎn)的Y坐標(biāo)。
proportionalBoolean
設(shè)置坐標(biāo)是否與形狀成比例。 true將使用單位正方形坐標(biāo),否則使用屏幕坐標(biāo)系。
cycleMethodCycleMethod
設(shè)置應(yīng)用于漸變的循環(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);
    }
}

上面的代碼生成以下結(jié)果。

null

徑向梯度

下表列出了RadialGradient屬性。

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

    }
}

上面的代碼生成以下結(jié)果。

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

上面的代碼生成以下結(jié)果。

null

反射循環(huán)漸變

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

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

CycleMethod.REFLECT使梯度圖案在停止顏色之間重復(fù)或循環(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);
  }
}

上面的代碼生成以下結(jié)果。

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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)