JavaFX | ColorInput Class

ColorInput class is a part of JavaFX. It is used to create an effect which renders a rectangular region, filled with the given Color. It is equivalent to rendering a filled rectangle into an image and using an ImageInput effect, except that it is more convenient and potentially much more efficient. It is mainly passed into the other effects as an input.
Constructors of the class:
- ColorInput(): Creates a new instance of ColorInput with default parameters.
- ColorInput(double x, double y, double width, double height, Paint paint): Creates a new instance of ColorInput with the specified x, y, width, height, and paint.
Commonly Used Methods:
| Method | Description |
|---|---|
| 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 |
- Java program to Demonstrate ColorInput class: In this program, ColorInput Effect is created and then we set the color, height, width, and coordinates of the region of ColorInput. A group object and scene object is created. A scene is added to the stage and then we set the title of the stage.
// Java program to Demonstrate ColorInput classimportjavafx.application.Application;importjavafx.scene.Group;importjavafx.scene.Scene;importjavafx.scene.effect.ColorInput;importjavafx.scene.paint.Color;importjavafx.scene.shape.Rectangle;importjavafx.stage.Stage;publicclassColorInputDemoextendsApplication {// Main Methodpublicstaticvoidmain(String[] args){// launch the applicationlaunch(args);}// launch the applicationpublicvoidstart(Stage primaryStage)throwsException{// Instantiating the ColorInput classColorInput color =newColorInput();// set the colorcolor.setPaint(Color.GREEN);// sets the height of the region of color inputcolor.setHeight(50);// sets the width of the region of color inputcolor.setWidth(200);// set the coordinates of the Colorinputcolor.setX(90);color.setY(140);// create a rectangleRectangle rect =newRectangle();// applying coloradjust effectrect.setEffect(color);// create a group objectGroup root =newGroup();// create a scene objectScene scene =newScene(root,400,300);root.getChildren().add(rect);// adding scene to the stageprimaryStage.setScene(scene);// set title of the stageprimaryStage.setTitle("ColorInput Demo");primaryStage.show();}}Output:
- Java program to apply ColorInput class to the created rectangle by clicking on the button using EventHandler: In this program, we first set the Height, Width, and coordinates of a rectangle and then create a rectangle of the same dimension. Now, create a Button and set the Layouts of the Button. Now, using EventHandler, first, instantiate a ColorInput class using proper dimension and then set ColorInput Effect to the Button. Create a group object and add Button and rectangle to it. Then Create a Scene and add it to the stage.
// Java program to apply ColorInput class to// the created rectangle by clicking on the// button using EventHandlerimportjavafx.application.Application;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.Group;importjavafx.scene.Scene;importjavafx.scene.control.Button;importjavafx.scene.effect.ColorInput;importjavafx.scene.paint.Color;importjavafx.scene.shape.Rectangle;importjavafx.stage.Stage;publicclassColorInputExampleextendsApplication {publicvoidstart(Stage stage){doublex =10;doubley =10;doublew =40;doubleh =180;// RectangleRectangle rect =newRectangle(x, y, w, h);rect.setFill(Color.WHITE);rect.setStrokeWidth(1);rect.setStroke(Color.BLACK);// ButtonButton button =newButton("Click To See the Effects!");// set button layout coordinatesbutton.setLayoutX(100);button.setLayoutY(30);button.setPrefSize(250,150);button.setOnAction(newEventHandler<ActionEvent>() {publicvoidhandle(ActionEvent event){// instantiating the colorinput classColorInput colorInput =newColorInput(x, y,w, h, Color.STEELBLUE);// Setting ColorInput effectbutton.setEffect(colorInput);}});// create the Group objectGroup root =newGroup();root.getChildren().addAll(button, rect);Scene scene =newScene(root,450,300);stage.setTitle("JavaFX ColorInput Effect");stage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String args[]){// Launch the applicationlaunch(args);}}Output:
Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javafx/2/api/javafx/scene/effect/ColorInput.html




