鍍金池/ 教程/ Java/ JavaFX顏色
安裝e(fx)clipse到Eclipse (JavaFX工具)
JavaFX屬性
JavaFX文本域(輸入框)
JavaFX切換按鈕
JavaFX曲線
JavaFX教程
JavaFX菜單(Menu)
JavaFX快速入門(mén)
JavaFX復(fù)選框
JavaFX ScrollPane布局
JavaFX綁定
JavaFX顏色選擇器(ColorPicker)
JavaFX進(jìn)度指示器
JavaFX按鈕
JavaFX TitledPane布局
JavaFX圓弧
JavaFX開(kāi)發(fā)環(huán)境安裝配置
在Eclipse安裝JavaFX Scene Builder
JavaFX路徑
JavaFX VBox
JavaFX線條
JavaFX漸變顏色
JavaFX集合
JavaFX BorderPane布局
JavaFX DatePicker
JavaFX單選按鈕
JavaFX滾動(dòng)條
JavaFX矩形橢圓
JavaFX GridPane布局
JavaFX HBox
JavaFX進(jìn)度條
JavaFX多邊形折線
JavaFX超鏈接
JavaFX密碼字段
JavaFX Accordion布局
JavaFX概述和簡(jiǎn)介
JavaFX選擇框
JavaFX文本
JavaFX顏色
JavaFX文件選擇器(FileChooser)
JavaFX標(biāo)簽
JavaFX FlowPane布局

JavaFX顏色

JavaFX中,我們可以在對(duì)象上應(yīng)用顏色(Paint)。在JavaFX中,所有形狀都可以填充簡(jiǎn)單的顏色和漸變顏色。

RGB顏色

當(dāng)指定顏色值時(shí),可以使用默認(rèn)的RGB顏色空間中的顏色。
要?jiǎng)?chuàng)建顏色,請(qǐng)使用Color.rgb()方法。此方法使用三個(gè)整數(shù)值,表示紅色,綠色和藍(lán)色分量。請(qǐng)閱讀以下一段簡(jiǎn)單的代碼 -

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;

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

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Drawing Text");
        Group root = new Group();
        Scene scene = new Scene(root, 300, 250, Color.WHITE);
        int x = 100;
        int y = 100;
        int red = 30;
        int green = 40;
        int blue = 50;

        Text text = new Text(x, y, "JavaFX 2.0");

        text.setFill(Color.rgb(red, green, blue, .99));
        text.setRotate(60);
        root.getChildren().add(text);


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

執(zhí)行上面的代碼,得到以下結(jié)果 -

顏色名稱(chēng)

以下代碼根據(jù)顏色名稱(chēng)創(chuàng)建顏色。如:Color.DARKBLUE,請(qǐng)參閱如下代碼 -

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Title");

        final Circle circ = new Circle(40, 40, 30);
        final Group root = new Group(circ);

        final Scene scene = new Scene(root, 800, 400, Color.BEIGE);

        final Text text1 = new Text(25, 25, "yiibai.com");
        text1.setFill(Color.DARKBLUE);
        text1.setFont(Font.font(java.awt.Font.SERIF, 25));
        root.getChildren().add(text1);

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

執(zhí)行上面的代碼,得到以下結(jié)果 -

顏色alpha通道

另一個(gè)重載方法需要三個(gè)整數(shù)(int)值和第四個(gè)double類(lèi)型值,即alpha通道。第四個(gè)值設(shè)置顏色的不透明度。此值介于零(0)和一(1)之間。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Text Fonts");
        Group root = new Group();
        Scene scene = new Scene(root, 550, 250, new Color(0,0,1,1.0));

        Text text = new Text(50, 100, "JavaFX 2.0 from yiibai.com");
        Font sanSerif = Font.font("Dialog", 30);
        text.setFont(sanSerif);
        text.setFill(Color.RED);
        root.getChildren().add(text);


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

執(zhí)行上面的代碼,得到以下結(jié)果 -

HSB顏色

還可以通過(guò)指定色相,飽和度和亮度(HSB)來(lái)創(chuàng)建顏色。 要使用HSB創(chuàng)建顏色,請(qǐng)使用Color.hsb()方法。

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Text Fonts");
        Group root = new Group();
        Scene scene = new Scene(root, 550, 250,Color.hsb(270,1.0,1.0,1.0));

        Text text = new Text(50, 100, "JavaFX 2.0 from yiibai.com");
        Font sanSerif = Font.font("Dialog", 30);
        text.setFont(sanSerif);
        text.setFill(Color.RED);
        root.getChildren().add(text);


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

執(zhí)行上面的代碼,得到以下結(jié)果 -

Web顏色

以下代碼顯示了如何從web值來(lái)創(chuàng)建顏色。

 Color c = Color.web("#0000FF",1.0);// blue as a hex web value, explict alpha
 Color c = Color.web("#0000FF");// blue as a hex web value, implict alpha
 Color c = Color.web("0000FF",1.0);// blue as a hex web value, explict alpha
 Color c = Color.web("0000FF");// blue as a hex web value, implict alpha

請(qǐng)參閱如下代碼 -

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {
  public static void main(String[] args) {
    launch(args);
  }

  @Override
  public void start(Stage stage) {
    Scene scene = new Scene(new Group());
    stage.setTitle("Label Sample");
    stage.setWidth(400);
    stage.setHeight(180);

    HBox hbox = new HBox();

    Label label1 = new Label("Search");
    label1.setTextFill(Color.web("#0076a3"));

     hbox.setSpacing(10);
    hbox.getChildren().add((label1));
    ((Group) scene.getRoot()).getChildren().add(hbox);

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

要使用RGB十六進(jìn)制值作為CSS指定顏色值,可以使用Color.web()方法。執(zhí)行上面的代碼,得到以下結(jié)果 -