例子
以下是演示颜色输入效果的示例。在这里,我们在位置 50、140 处创建尺寸为 50、400(高度、宽度)的颜色输入,并用颜色 CHOCOLATE 填充它。
我们正在创建矩形并将此效果应用于它。将此代码保存在名称为的文件中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 文件。
javac ColorInputEffectExample.java
java ColorInputEffectExample
执行时,上述程序会生成一个 JavaFX 窗口,如下所示。