Javafx 简明教程
JavaFX - Color Input Effect
颜色输入效果给出的输出与绘制一个矩形并用颜色填充它相同。与其他效果不同,如果此效果应用于任何节点,那么它只显示一个矩形框(而不是节点)。此效果最常用于作为其他效果的输入传递。
Color Input Effect gives the same output as drawing a rectangle and filling it with color. Unlike other effects, if this effect is applied to any node, it displays only a rectangular box (not the node). This effect is mostly used to pass as an input for other effects.
例如,在应用混合效果时,它需要将效果类型对象作为输入。在那里我们可以将其作为输入传递。
For example, while applying the blend effect, it requires an object of effect type as input. There we can pass this as an input.
包 javafx.scene.effect 中名为 ColorInput 的类表示颜色输入效果。此类包含四个属性,即 −
The class named ColorInput of the package javafx.scene.effect represents the color input effect. This class contains four properties namely −
-
x − This property is of double type; it represents the x coordinate of the position of the color input.
-
y − This property is of double type; it represents the y coordinate of the position of the color input.
-
height − This property is of double type; it represents the height of the region that is to be filled with color.
-
width − This property is of double type; it represents the width of the region that is to be filled with color.
-
paint − This property is of Paint type; it represents the color with which the input region is to be filled.
Example
下面是一个演示颜色输入效果的示例。在这里,我们在位置 50, 140 处创建一个高度 50、宽度 400(高度、宽度)的颜色输入,并用 CHOCOLATE 颜色填充它。
Following is an example demonstrating the color input effect. In here, we are creating a color input of the dimensions 50, 400 (height, width) at the position 50, 140, and filling it with the color CHOCOLATE.
我们正在创建矩形并对其应用此效果。将此代码保存在名为 ColorInputEffectExample.java. 的文件中
We are creating rectangle and applying this effect to it. Save this code in a file with the name ColorInputEffectExample.java.
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 文件。
Compile and execute the saved java file from the command prompt using the following commands.
javac --module-path %PATH_TO_FX% --add-modules javafx.controls ColorInputEffectExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls ColorInputEffectExample