个性化阅读
专注于IT技术分析

JavaFX颜色

本文概述

在JavaFX中, 我们可以用颜色填充形状。我们可以灵活地使用几种方法创建自己的颜色, 并将其作为Paint对象传递给setFill()方法。让我们讨论一下JavaFX中创建颜色的几种方法。

RGB颜色

RGB颜色系统是在图形中创建颜色的最流行方法。它由名为RED→R, GREEN→G和BLUE→B的三个组件组成。每个组件使用8位, 这意味着每个组件都可以具有从0到22 ^ 8-1 = 255的整数值。

电脑屏幕可以看作是像素的集合。集合(R, G, B)实际上表示屏幕上相应LED的发射。

如果将RED的值设置为0, 则表示红色LED熄灭, 而值255表示LED处于完全发光状态。 (0, 0, 0)的组合表示黑色, 而(255, 255, 255)的组合表示白色。该范围内的中间值可以代表不同的颜色。

使用RGB的叠加, 我们可以表示255 * 255 * 255种不同的颜色。在JavaFX中, 类javafx.scene.paint.Color类表示颜色。

有一个名为Color类的rgb()的静态方法。它接受三个整数参数, 例如Red, Green, Blue和一个可选的double参数, 称为alpha。 alpha的值与颜色的不透明度成正比。 Alpha值0表示颜色是完全透明的, 而值1表示颜色是完全不透明的。

例:

package application;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Shadow;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Shape_Example extends Application {

  @Override
  public void start(Stage primarystage) {
    Group root = new Group();
    primarystage.setTitle("Color Example");
    Rectangle rect = new Rectangle();
    rect.setX(50);
    rect.setY(20);
    rect.setWidth(100);
    rect.setHeight(150);
    int red=20;
    int green=125;
    int blue=10;
    rect.setFill(Color.rgb(red, green, blue, 0.63));
    root.getChildren().add(rect);
    Scene scene = new Scene(root, 200, 200);
    primarystage.setScene(scene);
    primarystage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}
JavaFX颜色输出1

颜色名称

在JavaFX中, 我们还可以通过颜色名称创建颜色。类javafx.scene.paint.Color包含所有颜色作为类属性。需要在setFill()方法中将Color属性作为Paint类对象传递。

例:

package application;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Shadow;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Shape_Example extends Application {

  @Override
  public void start(Stage primarystage) {
    Group root = new Group();
    primarystage.setTitle("Color Example");
    Rectangle rect = new Rectangle();
    rect.setX(50);
    rect.setY(20);
    rect.setWidth(100);
    rect.setHeight(150);
    rect.setFill(Color.RED); //passing color name 
    rect.setEffect(new DropShadow());
    root.getChildren().add(rect);
    Scene scene = new Scene(root, 200, 200);
    primarystage.setScene(scene);
    primarystage.show();
  }

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

HSB颜色

除了到目前为止我们已经看到的各种方法之外, JavaFX还使我们能够使用HSB来创建颜色, HSB是色相, 饱和度和亮度的组合。 javafx.scene.paint.Color包含一个静态方法Color.hsb(), 该方法接受三个整数h, s和b。

例:

下面的示例实现Color.hsb()方法来填充舞台。

package application;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Shape_Example extends Application {

  @Override
  public void start(Stage primarystage) {
    Group root = new Group();
    primarystage.setTitle("Color Example");
    Rectangle rect = new Rectangle();
    rect.setX(50);
    rect.setY(20);
    rect.setWidth(200);
    rect.setHeight(250);
    rect.setEffect(new DropShadow());
    root.getChildren().add(rect);
    Scene scene = new Scene(root, 300, 400, Color.hsb(180, 1, 1));
    primarystage.setScene(scene);
    primarystage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}
JavaFX颜色输出3

网页颜色

javafx.scene.paint.color类使我们能够使用Color.web()方法创建Web颜色。这最多可以使用两个参数, 其中一个是颜色的十六进制值, 另一个是称为alpha通道的可选参数, 它代表颜色的不透明度。

Color.web(“#0000FF”)//带有隐式alpha的蓝色

Color.web(“#0000FF”, 1)//带有明确Alpha的蓝色

Alpha是双精度类型的值, 可以将值保留在0.0到1.0的范围内。

例:

package application;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class Shape_Example extends Application {

  @Override
  public void start(Stage primarystage) {
    Group root = new Group();
    primarystage.setTitle("Color Example");
    Rectangle rect = new Rectangle();
    rect.setX(50);
    rect.setY(20);
    rect.setWidth(200);
    rect.setHeight(250);
    rect.setEffect(new DropShadow());
    rect.setFill(Color.web("#0000FF", 1));
    root.getChildren().add(rect);
    Scene scene = new Scene(root, 300, 400);
    primarystage.setScene(scene);
    primarystage.show();
  }

  public static void main(String[] args) {
    launch(args);
  }
}
JavaFX颜色输出4
赞(0)
未经允许不得转载:srcmini » JavaFX颜色

相关推荐

评论 抢沙发

评论前必须登录!