Friday 30 October 2020

JavaFX: Apply Blend effect

 

Blend effect is used to combine two inputs together using specified blend mode.

 

'Blend' class is used to define Blend instance.

 

public Blend(BlendMode mode)

public Blend(BlendMode mode, Effect bottomInput, Effect topInput)

mode: specifies the BlendMode used to blend the two inputs together

bottomInput: specifies the bottom input for this Blend operation

topInput: specifies the top input for this Blend operation

 

BlendMode

BlendMode is an enum which is used to specify blending mode defines the manner in which the inputs of a Blend effect are composited together or how a Node is blended into the background of a scene.

 

Following table summarizes different blend modes and their behaviour.

 

BlendMode

Description

SRC_OVER

The top input is blended over the bottom input. (Equivalent to the Porter-Duff "source over destination" rule.)

SRC_ATOP

The part of the top input lying inside of the bottom input is blended with the bottom input. (Equivalent to the Porter-Duff "source atop destination" rule.)

ADD

The color and alpha components from the top input are added to those from the bottom input. The result is clamped to 1.0 if it exceeds the logical

MULTIPLY

The color components from the first input are multiplied with those from the second input. The alpha components are blended according to the SRC_OVER equation.

SCREEN

The color components from both of the inputs are inverted, multiplied with each other, and that result is again inverted to produce the resulting color. The alpha components are blended according to the SRC_OVER equation.

OVERLAY

The input color components are either multiplied or screened, depending on the bottom input color. The alpha components are blended according to the SRC_OVER equation.

DARKEN

The darker of the color components from the two inputs are selected to produce the resulting color. The alpha components are blended according to the SRC_OVER equation.

LIGHTEN

The lighter of the color components from the two inputs are selected to produce the resulting color. The alpha components are blended according to the SRC_OVER equation.

COLOR_DODGE

The bottom input color components are divided by the inverse of the top input color components to produce the resulting color. The alpha components are blended according to the SRC_OVER equation.

COLOR_BURN

The inverse of the bottom input color components are divided by the top input color components, all of which is then inverted to produce the resulting color. The alpha components are blended according to the SRC_OVER equation.

HARD_LIGHT

The input color components are either multiplied or screened, depending on the top input color. The alpha components are blended according to the SRC_OVER equation.

SOFT_LIGHT

The input color components are either darkened or lightened, depending on the top input color. The alpha components are blended according to the SRC_OVER equation.

DIFFERENCE

The darker of the color components from the two inputs are subtracted from the lighter ones to produce the resulting color. The alpha components are blended according to the SRC_OVER equation.

EXCLUSION

The color components from the two inputs are multiplied and doubled, and then subtracted from the sum of the bottom input color components, to produce the resulting color. The alpha components are blended according to the SRC_OVER equation.

RED

The red component of the bottom input is replaced with the red component of the top input; the other color components are unaffected. The alpha components are blended according to the SRC_OVER equation.

GREEN

The green component of the bottom input is replaced with the green component of the top input; the other color components are unaffected. The alpha components are blended according to the SRC_OVER equation.

BLUE

The blue component of the bottom input is replaced with the blue component of the top input; the other color components are unaffected. The alpha components are blended according to the SRC_OVER equation.

 

How to apply blend affect?

Create an instance of Blend and apply it to the widget.

 

Example

Blend blendEffect = new Blend();

blendEffect.setMode(BlendMode.ADD);

 

group1.setEffect(blendEffect);

 

Find the below working application.

 

blendDemo.fxml 

<?import javafx.scene.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<Group xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.controller.BlendController" fx:id="group1" >

	<Circle fx:id="circle1" centerX="200" centerY="150" radius="50" fill="red" opacity="1.0"/>
	
	<Circle fx:id="circle2" centerX="150" centerY="100" radius="50" fill="green" opacity="0.5"/>
	
	<Circle fx:id="circle3" centerX="250" centerY="100" radius="50" fill="blue" opacity="0.5"/>
		
</Group>

 

BlendController.java

package com.sample.app.controller;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Group;
import javafx.scene.effect.Blend;
import javafx.scene.effect.BlendMode;
import javafx.scene.shape.Circle;

public class BlendController implements Initializable {

	@FXML
	private Group group1;
	
	@FXML
	private Circle circle1;
	
	@FXML
	private Circle circle2;
	
	@FXML
	private Circle circle3;
	

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		Blend blendEffect = new Blend();
		blendEffect.setMode(BlendMode.ADD);

		group1.setEffect(blendEffect);

	}

}

 

BlendEffectDemo.java

package com.sample.app.effects;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class BlendEffectDemo extends Application {

	private static final String FXML_FILE = "/blendDemo.fxml";
	private static final String STAGE_TITLE = "Blend Effect";

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

	}

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

		Parent root = (Parent) FXMLLoader.load(this.getClass().getResource(FXML_FILE));

		Scene scene = new Scene(root, 400, 400, Color.WHITE);

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

}

Output

 


 

Note

Opacity value should be less than zero to perform blending.

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment