Saturday, 7 November 2020

JavaFX: Create chain of effects

Output of one effect can be set as input to other effect. Using this chaining, we can apply multiple effects to the node.

 

In this post, I am going to apply reflection affect as input to the PerspectiveTransform effect which in turn effect an image.

 

Step 1: Create Reflection effect.

Reflection reflection = new Reflection(0.3, 1.0, 0.2, 0.5);

 

Step 2: Create PerspectiveTransform instance and set reflection as input to PerspectiveTransform effect.

PerspectiveTransform perspectiveTrasform = new PerspectiveTransform();

perspectiveTrasform.setUlx(10.0);

perspectiveTrasform.setUly(10.0);

perspectiveTrasform.setUrx(350.0);

perspectiveTrasform.setUry(100.0);

perspectiveTrasform.setLrx(400.0);

perspectiveTrasform.setLry(450.0);

perspectiveTrasform.setLlx(10.0);

perspectiveTrasform.setLly(550.0);

 

perspectiveTrasform.setInput(reflection);

 

Step 3: Create an ImageView and set the perspectiveTrasform effect to this image view

String filename = "house.png";

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

 

Image image = new Image(in);

ImageView imageView = new ImageView(image);

imageView.setFitWidth(400);

imageView.setFitHeight(400);

imageView.setPreserveRatio(true);

imageView.setEffect(perspectiveTrasform);

 

Step 4: Add imageView instance to Group.

Group g = new Group();

 

g.getChildren().addAll(imageView);

 

Step 5: Set Scene and stage.

Scene scene = new Scene(g, 500, 600, Color.WHITE);

 

primaryStage.setTitle(STAGE_TITLE);

primaryStage.setScene(scene);

primaryStage.show();

 

house.png


Find the below working application.

 

ChainEffectDemo.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.PerspectiveTransform;
import javafx.scene.effect.Reflection;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class ChainEffectDemo extends Application {

	private static final String STAGE_TITLE = "Chain effect Demo";

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

	}

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

		Reflection reflection = new Reflection(0.3, 1.0, 0.2, 0.5);

		PerspectiveTransform perspectiveTrasform = new PerspectiveTransform();
		perspectiveTrasform.setUlx(10.0);
		perspectiveTrasform.setUly(10.0);
		perspectiveTrasform.setUrx(350.0);
		perspectiveTrasform.setUry(100.0);
		perspectiveTrasform.setLrx(400.0);
		perspectiveTrasform.setLry(450.0);
		perspectiveTrasform.setLlx(10.0);
		perspectiveTrasform.setLly(550.0);

		perspectiveTrasform.setInput(reflection);

		String filename = "house.png";
		InputStream in = this.getClass().getClassLoader().getResourceAsStream(filename);

		Image image = new Image(in);
		ImageView imageView = new ImageView(image);
		imageView.setFitWidth(400);
		imageView.setFitHeight(400);
		imageView.setPreserveRatio(true);
		imageView.setEffect(perspectiveTrasform);

		Group g = new Group();

		g.getChildren().addAll(imageView);

		Scene scene = new Scene(g, 500, 600, Color.WHITE);

		primaryStage.setTitle(STAGE_TITLE);
		primaryStage.setScene(scene);
		primaryStage.show();
	}

}

Output




 

Previous                                                    Next                                                    Home

No comments:

Post a Comment