Creating Visual Effects in JavaFX

Previous
Next

6 Adding a Reflection

Reflection is an effect that renders a reflected version of the object below the actual object.


Note:

The reflection of a node with a reflection effect will not respond to mouse events or the containment methods on the node.


Figure 6-1 shows a reflection applied to text. Use the setFraction method to specify the amount of visible reflection.

Figure 6-1 Reflection Effect

Description of Figure 6-1 follows
Description of "Figure 6-1 Reflection Effect"

Example 6-1 shows how to create the reflection effect on text.

Example 6-1 Text With Reflection

import javafx.scene.text.*;
import javafx.scene.paint.*;
import javafx.scene.effect.*;
public class HelloEffects extends Application {
 
    Stage stage;
    Scene scene;

    @Override public void start(Stage stage) {
        stage.show();
 
        scene = new Scene(new Group(), 840, 680);
        ObservableList<Node> content = ((Group)scene.getRoot()).getChildren();
        content.add(reflection());
        stage.setScene(scene);
    }
    static Node reflection() {
        Text t = new Text();
        t.setX(10.0f);
        t.setY(50.0f);
        t.setCache(true);
        t.setText("Reflection in JavaFX...");
        t.setFill(Color.RED);
        t.setFont(Font.font("null", FontWeight.BOLD, 30));
 
        Reflection r = new Reflection();
        r.setFraction(0.9);
 
        t.setEffect(r);
 
        t.setTranslateY(400);
        return t;
    }
    public static void main(String[] args) {
        Application.launch(args);
    }
}
Previous
Next