多读书多实践,勤思考善领悟

JavaFX effect ColorInput(颜色输入效果)

本文于1665天之前发表,文中内容可能已经过时。

颜色输入效果提供与绘制矩形并用颜色填充相同的输出。与其他效果不同,如果此效果应用于任何节点,则它仅显示矩形框(而不是节点)。此效果主要用于传递作为其他效果的输入。

例如,在应用混合效果时,它需要效果类型的对象作为输入。我们可以将此作为输入传递。

javafx.scene.effect的名为ColorInput的类表示颜色输入效果。该类包含四个属性,即 -

  • x - 此属性为双重类型; 它表示颜色输入位置的x坐标。
  • y - 此属性为双重类型; 它表示颜色输入位置的y坐标。
  • 身高 - 此属性为双重类型; 它代表要填充颜色的区域的高度。
  • width - 此属性为double类型; 它表示要用颜色填充的区域的宽度。
  • paint - 此属性为Paint类型; 它表示输入区域的填充颜色。

以下是演示颜色输入效果的示例。在这里,我们在位置50,140处创建尺寸为50,400(高度,宽度)的颜色输入,并用颜色CHOCOLATE填充它。

我们正在创建矩形并将此效果应用于它。将此代码保存在名为ColorInputEffectExample.java的文件中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import javafx.application.Application; 
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.ColorInput;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class ColorInputEffectExample extends Application {
@Override
public void start(Stage stage) {
//creating a rectangle
Rectangle rectangle = new Rectangle();

//Instantiating the Colorinput class
ColorInput colorInput = new ColorInput();

//Setting the coordinates of the color input
colorInput.setX(50);
colorInput.setY(140);

//Setting the height of the region of the collor input
colorInput.setHeight(50);

//Setting the width of the region of the color input
colorInput.setWidth(400);

//Setting the color the color input
colorInput.setPaint(Color.CHOCOLATE);

//Applying coloradjust effect to the Rectangle
rectangle.setEffect(colorInput);

//Creating a Group object
Group root = new Group(rectangle);

//Creating a scene object
Scene scene = new Scene(root, 600, 300);

//Setting title to the Stage
stage.setTitle("Sample Application");

//Adding scene to the stage
stage.setScene(scene);

//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}

使用以下命令从命令提示符编译并执行保存的java文件。

1
2
javac ColorInputEffectExample.java 
java ColorInputEffectExample

执行时,上面的程序生成一个JavaFX窗口,如下所示。

颜色输入效果