在JavaFX中,我們可以在對(duì)象上應(yīng)用顏色(Paint)。在JavaFX中,所有形狀都可以填充簡(jiǎn)單的顏色和漸變顏色。
當(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é)果 -
以下代碼根據(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é)果 -
另一個(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é)果 -
還可以通過(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值來(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é)果 -