📜  JavaFX |颜色输入类

📅  最后修改于: 2022-05-13 01:54:55.865000             🧑  作者: Mango

JavaFX |颜色输入类

ColorInput 类是 JavaFX 的一部分。它用于创建渲染矩形区域的效果,填充给定的颜色。它相当于将填充的矩形渲染成图像并使用 ImageInput 效果,只是它更方便并且可能更高效。它主要作为输入传递给其他效果。

类的构造函数:

  1. ColorInput() :使用默认参数创建一个新的 ColorInput 实例。
  2. ColorInput(double x, double y, double width, double height, Paint paint) :使用指定的 x、y、宽度、高度和油漆创建 ColorInput 的新实例。

常用方法:

MethodDescription
getX()Gets the value of the property x.
getY()Gets the value of the property y.
setHeight(double value)Sets the value of the property height
setPaint(Paint value)Sets the value of the property paint.
setWidth(double value)Sets the value of the property width.
setX(double value)Sets the value of the property x.
setY(double value)Sets the value of the property y.
getHeight()Gets the value of the property height.
getPaint()Gets the value of the property paint.
getWidth()Gets the value of the property width
  1. 演示 ColorInput 类的Java程序:在这个程序中,创建了ColorInput Effect ,然后我们设置 ColorInput 区域的颜色、高度、宽度和坐标。创建组对象和场景对象。一个场景被添加到舞台,然后我们设置舞台的标题。
    // Java program to Demonstrate ColorInput class
    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 ColorInputDemo extends Application {
      
        // Main Method 
        public static void main(String[] args)
        {
      
            // launch the application
            launch(args);
        }
      
        // launch the application
        public void start(Stage primaryStage) throws Exception
        {
      
            // Instantiating the ColorInput class
            ColorInput color = new ColorInput();
      
            // set the color
            color.setPaint(Color.GREEN);
      
            // sets the height of the region of color input
            color.setHeight(50);
      
            // sets the width of the region of color input
            color.setWidth(200);
      
            // set the coordinates of the Colorinput
            color.setX(90);
            color.setY(140);
      
            // create a rectangle
            Rectangle rect = new Rectangle();
      
            // applying coloradjust effect
            rect.setEffect(color);
      
            // create a group object
            Group root = new Group();
      
            // create a scene object
            Scene scene = new Scene(root, 400, 300);
      
            root.getChildren().add(rect);
      
            // adding scene to the stage
            primaryStage.setScene(scene);
      
            // set title of the stage
            primaryStage.setTitle("ColorInput Demo");
      
            primaryStage.show();
        }
    }
    

    输出:

  2. Java程序通过使用 EventHandler 单击按钮将 ColorInput 类应用于创建的矩形:在此程序中,我们首先设置矩形的高度、宽度和坐标,然后创建一个相同尺寸的矩形。现在,创建一个按钮并设置按钮的布局。现在,使用 EventHandler,首先,使用适当的维度实例化一个 ColorInput 类,然后将 ColorInput Effect 设置为 Button。创建一个组对象并向其添加按钮和矩形。然后创建一个场景并将其添加到舞台。
    // Java program to apply ColorInput class to 
    // the created rectangle by clicking on the 
    // button using EventHandler
    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.effect.ColorInput;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Rectangle;
    import javafx.stage.Stage;
      
    public class ColorInputExample extends Application {
      
        public void start(Stage stage)
        {
      
            double x = 10;
            double y = 10;
            double w = 40;
            double h = 180;
      
            // Rectangle
            Rectangle rect = new Rectangle(x, y, w, h);
            rect.setFill(Color.WHITE);
            rect.setStrokeWidth(1);
            rect.setStroke(Color.BLACK);
      
            // Button
            Button button = new Button("Click To See the Effects!");
      
            // set button layout coordinates
            button.setLayoutX(100);
            button.setLayoutY(30);
      
            button.setPrefSize(250, 150);
      
            button.setOnAction(new EventHandler() {
      
                public void handle(ActionEvent event)
                {
                    // instantiating the colorinput class
                    ColorInput colorInput = new ColorInput(x, y, 
                                         w, h, Color.STEELBLUE);
      
                    // Setting ColorInput effect
                    button.setEffect(colorInput);
                }
            });
      
            // create the Group object
            Group root = new Group();
      
            root.getChildren().addAll(button, rect);
      
            Scene scene = new Scene(root, 450, 300);
            stage.setTitle("JavaFX ColorInput Effect");
            stage.setScene(scene);
      
            stage.show();
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // Launch the application
            launch(args);
        }
    }
    

    输出:

注意:上述程序可能无法在在线 IDE 中运行。请使用离线编译器。

参考: https://docs.oracle.com/javafx/2/api/javafx/scene/effect/ColorInput.html