Thursday 5 November 2020

JavaFX: ColorInput Effect

ColorInput effect is used to render a rectangular region that is filled with the given color.

 

ColorInput serves as an effect to other effect instance.

 

house.png

 


 

Let’s apply ColorInput effect on above image.

 

Step 1: Define an Image instance.

 

String filename = "house.png";

InputStream in = this.getClass().getClassLoader().getResourceAsStream(filename);

Image image = new Image(in, 350, 350, true, true);

Step 2: Define ImageInput instance from the Image instance.

ImageInput imageInput = new ImageInput(image, 50, 50);

 

Step 3: Define BoxBlur instance.

BoxBlur boxBlur = new BoxBlur(5, 10, 10);

boxBlur.setInput(imageInput);

 

Step 4: Define Group and set BoxBlur effect.

Group group = new Group();

group.setEffect(boxBlur);

 

Step 5: Define a Scene instance and attach Group instance as root node.

Scene scene = new Scene(group, 400, 400);

 

Step 6: Set the scene to stage and display the window by calling show function.

primaryStage.setTitle("Image Input Demo");

primaryStage.setScene(scene);

primaryStage.show();

 

Find the below working application.

 

ImageInputEffectDemo.java

package com.sample.app.effects;

import java.io.InputStream;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.BoxBlur;
import javafx.scene.effect.ImageInput;
import javafx.scene.image.Image;
import javafx.stage.Stage;

public class ImageInputEffectDemo extends Application {

	@Override
	public void start(Stage primaryStage) throws Exception {

		String filename = "house.png";
		InputStream in = this.getClass().getClassLoader().getResourceAsStream(filename);
		Image image = new Image(in, 350, 350, true, true);

		ImageInput imageInput = new ImageInput(image, 50, 50);

		BoxBlur boxBlur = new BoxBlur(5, 10, 10);
		boxBlur.setInput(imageInput);

		Group group = new Group();
		group.setEffect(boxBlur);

		Scene scene = new Scene(group, 400, 400);
		primaryStage.setTitle("Image Input Demo");
		primaryStage.setScene(scene);
		primaryStage.show();

	}

	public static void main(String args[]) {
		launch(args);
	}

}


Output


 


Previous                                                    Next                                                    Home

No comments:

Post a Comment