Wednesday, 28 October 2020

JavaFX: BoxBlur: Create Box Blur effect

BoxBlur class is used to create box blur effect using a simple box filter kernel, with separately configurable sizes in both dimensions, and an iteration parameter that controls the quality of the resulting blur.

 

BoxBlur class provides following constructors to define BoxBlur instance.

 

public BoxBlur()

public BoxBlur(double width, double height, int iterations)

width: the horizontal dimension of the blur effect. Width specifies horizontal blurness.

height: the vertical dimension of the blur effect. ‘height’ specifies vertical blurness.

iterations: the number of times to iterate the blur effect to improve its "quality" or "smoothness"

 

FXMl to blur a Text control

    <Text fx:id = "text1" text="Learning JavaFX"  fill="white"

        style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"

        stroke="black" strokeWidth="2"

        x="50" y = "50" effect="$shadow1">

       

        <BoxBlur fx:id = "shadow1"

            width="10"

            height="5"

            iterations="2"/>

    </Text>

 

Find the below working application.

 

boxBlurDemo.fxml

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

<Group xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.effects.controller.InnerShadowEffectController">

	<Text fx:id = "text1" text="Learning JavaFX"  fill="white"
		style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
		stroke="black" strokeWidth="2" 
		x="50" y = "50" effect="$shadow1">
		
		<BoxBlur fx:id = "shadow1" 
			width="10" 
			height="5"
			iterations="2"/>
	</Text>
	
	
	<Text fx:id = "text2" text="Learning JavaFX"  fill="white"
		style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
		stroke="black" strokeWidth="2" 
		x="50" y = "150" effect="$shadow1">
		
		<BoxBlur fx:id = "shadow1" 
			width="5" 
			height="10"
			iterations="2"/>
	</Text>
	
	
	<Text fx:id = "text3" text="Learning JavaFX"  fill="white"
		style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
		stroke="black" strokeWidth="2" 
		x="50" y = "250" effect="$shadow1">
		
		<BoxBlur fx:id = "shadow1" 
			width="10" 
			height="5"
			iterations="1"/>
	</Text>
	
	
	<Text fx:id = "text4" text="Learning JavaFX"  fill="white"
		style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
		stroke="black" strokeWidth="2" 
		x="50" y = "350" effect="$shadow1">
		
		<BoxBlur fx:id = "shadow1" 
			width="5" 
			height="10"
			iterations="1"/>
	</Text>
	
</Group>

 

BoxBlurController.java

package com.sample.app.effects.controller;

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

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.text.Text;

public class BoxBlurController  implements Initializable {

	@FXML
	private Text text1;
	
	@FXML
	private Text text2;
	
	@FXML
	private Text text3;
	
	@FXML
	private Text text4;

	@Override
	public void initialize(URL location, ResourceBundle resources) {

	}

}

 

BoxBlurDemo.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 BoxBlurDemo extends Application {

    private static final String FXML_FILE = "/boxBlurDemo.fxml";
    private static final String STAGE_TITLE = "Drop Shadow 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, 600, 400, Color.WHITE);

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

}

 

Output

 


 

 

  

Previous                                                    Next                                                    Home

JavaFx: InnerShadow effect

 InnerShadow is a high-level effect that renders a shadow inside the edges of the given content with the specified color, radius, and offset.

 

InnerShadow class provides following constructors, to get an InnerShadow instance.

 

public InnerShadow()

public InnerShadow(double radius, Color color)

public InnerShadow(double radius, double offsetX, double offsetY, Color color)

public InnerShadow(BlurType blurType, Color color, double radius, double choke, double offsetX, double offsetY)

blurType: the algorithm used to blur the shadow

color: the shadow color

radius: the radius of the shadow blur kernel

choke: the portion of the radius where the contribution of the source material will be 100%

offsetX: the shadow offset in the x direction

offsetY: the shadow offset in the y direction

 

FXML Example

    <Text fx:id = "text1" text="Learning JavaFX"  fill="white"

        style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"

        stroke="black" strokeWidth="2"

        x="50" y = "50" effect="$shadow1">

       

        <InnerShadow fx:id = "shadow1" radius="5.0" color="gray"

            offsetX="6"

            offsetY="6"/>

    </Text>

 

innerShadow.fxml

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

<Group xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.effects.controller.InnerShadowEffectController">

    <Text fx:id = "text1" text="Learning JavaFX"  fill="white"
        style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
        stroke="black" strokeWidth="2" 
        x="50" y = "50" effect="$shadow1">
        
        <InnerShadow fx:id = "shadow1" radius="5.0" color="gray" 
            offsetX="6"
            offsetY="6"/>
    </Text>
    
    <Text fx:id = "text2" text="Learning JavaFX"  fill="white"
        style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
        stroke="black" strokeWidth="2" 
        x="50" y = "150" effect="$shadow1">
        
        <InnerShadow fx:id = "shadow1" radius="5.0" color="gray" 
            offsetX="6"
            offsetY="-6"/>
    </Text>
    
    <Text fx:id = "text3" text="Learning JavaFX"  fill="white"
        style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
        stroke="black" strokeWidth="2" 
        x="50" y = "250" effect="$shadow1">
        
        <InnerShadow fx:id = "shadow1" radius="5.0" color="gray" 
            offsetX="-6"
            offsetY="6"/>
    </Text>
    
    <Text fx:id = "text4" text="Learning JavaFX"  fill="white"
        style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
        stroke="black" strokeWidth="2" 
        x="50" y = "350" effect="$shadow1">
        
        <InnerShadow fx:id = "shadow1" radius="5.0" color="gray" 
            offsetX="-6"
            offsetY="-6"/>
    </Text>
    
    
</Group>

InnerShadowEffectController.java

package com.sample.app.effects.controller;

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

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.shape.Rectangle;

public class InnerShadowEffectController implements Initializable {

    @FXML
    private Rectangle rectangle1;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

}


InnerShadowDemo.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 InnerShadowDemo extends Application {

    private static final String FXML_FILE = "/innerShadow.fxml";
    private static final String STAGE_TITLE = "Inner Shadow 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, 1000, 400, Color.WHITE);

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

}


Output





 

 

  

Previous                                                    Next                                                    Home

JavaFx: DropShadow Effect

'DropShadow' class is used to renders a shadow of the given content behind the content with the specified color, radius, and offset.

 

DropShadow class provides below constructors to set effect.

 

public DropShadow()

public DropShadow(double radius, Color color)

public DropShadow(double radius, double offsetX, double offsetY, Color color)

public DropShadow(BlurType blurType, Color color, double radius, double spread, double offsetX, double offsetY)

radius: the radius of the shadow blur kernel

offsetX: the shadow offset in the x direction

offsetY: the shadow offset in the y direction

color: the shadow color

blurType: the algorithm used to blur the shadow

 

FXML to create a drop shadow effect on a Text UI control

    <Text fx:id = "text1" text="Learning JavaFX"  fill="white"

        style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"

        stroke="black" strokeWidth="2"

        x="50" y = "50" effect="$shadow1">

       

        <DropShadow fx:id = "shadow1" radius="5.0" color="gray"

            offsetX="6"

            offsetY="6"/>

    </Text>

 

Find the below working application.

 

dropShadow.fxml

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

<Group xmlns:fx="http://javafx.com/fxml" fx:controller="com.sample.app.effects.controller.DropShadowEffectController">

    <Text fx:id = "text1" text="Learning JavaFX"  fill="white"
        style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
        stroke="black" strokeWidth="2" 
        x="50" y = "50" effect="$shadow1">
        
        <DropShadow fx:id = "shadow1" radius="5.0" color="gray" 
            offsetX="6"
            offsetY="6"/>
    </Text>
    
    <Text fx:id = "text2" text="Learning JavaFX"  fill="white"
        style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
        stroke="black" strokeWidth="2" 
        x="50" y = "150" effect="$shadow1">
        
        <DropShadow fx:id = "shadow1" radius="5.0" color="gray" 
            offsetX="6"
            offsetY="-6"/>
    </Text>
    
    <Text fx:id = "text3" text="Learning JavaFX"  fill="white"
        style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
        stroke="black" strokeWidth="2" 
        x="50" y = "250" effect="$shadow1">
        
        <DropShadow fx:id = "shadow1" radius="5.0" color="gray" 
            offsetX="-6"
            offsetY="6"/>
    </Text>
    
    <Text fx:id = "text4" text="Learning JavaFX"  fill="white"
        style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
        stroke="black" strokeWidth="2" 
        x="50" y = "350" effect="$shadow1">
        
        <DropShadow fx:id = "shadow1" radius="5.0" color="gray" 
            offsetX="-6"
            offsetY="-6"/>
    </Text>
    
    
</Group>

 

DropShadowEffectController.java

package com.sample.app.effects.controller;

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

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.text.Text;

public class DropShadowEffectController implements Initializable {

    @FXML
    private Text text1;
    
    @FXML
    private Text text2;
    
    @FXML
    private Text text3;
    
    @FXML
    private Text text4;

    @Override
    public void initialize(URL location, ResourceBundle resources) {

    }

}

 

DropShadowDemo.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 DropShadowDemo extends Application {

    private static final String FXML_FILE = "/dropShadow.fxml";
    private static final String STAGE_TITLE = "Drop Shadow 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, 600, 400, Color.WHITE);

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

}

Output

 


 

 

  

Previous                                                    Next                                                    Home