Thursday, 5 November 2020

JavaFx: Reflections

 Reflection effect renders a reflected version of the input below the actual input content.

 

Reflection class provides following constructors to define Reflection instance.

 

public Reflection()

public Reflection(double topOffset, double fraction, double topOpacity, double bottomOpacity)

topOffset: specifies the distance between the bottom of the input and the top of the reflection

fraction: specifies the fraction of the input that is visible in the reflection

topOpacity: specifies the opacity of the reflection at its top extreme

bottomOpacity: specifies the opacity of the reflection at its bottom extreme

 

Example

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

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

         stroke="darkblue" strokeWidth="2"

         x = "50" y = "50"

         effect="$reflection1">

        

                  <Reflection fx:id = "reflection1" topOffset="0.3" fraction="1.0" topOpacity="0.2" bottomOpacity="0.5"/>

        

         </Text>

 

Find the below working application.

 

reflectionDemo.fxml

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

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

	<Text fx:id = "text1" text="Learning JavaFX"  fill="lightgreen"
	style="-fx-font-weight: bold; -fx-font-size: 50; -fx-font-family: Verdana; -fx-font-style: italic"
	stroke="darkblue" strokeWidth="2" 
	x = "50" y = "50"
	effect="$reflection1">
	
		<Reflection fx:id = "reflection1" topOffset="0.3" fraction="1.0" topOpacity="0.2" bottomOpacity="0.5"/>
	
	</Text>
		
</Group>

 

ReflectionController.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 ReflectionController implements Initializable {

	@FXML
	private Text text1;

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

	
	}

}

 

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

	private static final String FXML_FILE = "/reflectionDemo.fxml";
	private static final String STAGE_TITLE = "Reflection 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.LIGHTSKYBLUE);

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

}


Output





Note.

Reflection of a Node with a Reflection effect installed will not respond to mouse events or the containment methods on the Node.

Previous                                                    Next                                                    Home

No comments:

Post a Comment