在JavaFX中,我们可以在对象上应用颜色(Paint)。在JavaFX中,所有形状都可以填充简单的颜色和渐变颜色。
RGB颜色
当指定颜色值时,可以使用默认的RGB颜色空间中的颜色。
要创建颜色,请使用Color.rgb()方法。此方法使用三个整数值,表示红色,绿色和蓝色分量。请阅读以下一段简单的代码 -
RGB对照表:RGB颜色对照表
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();
}
}