Skip to content

Instantly share code, notes, and snippets.

@jewelsea
Created March 22, 2013 00:13
Show Gist options
  • Save jewelsea/5217972 to your computer and use it in GitHub Desktop.
Save jewelsea/5217972 to your computer and use it in GitHub Desktop.
Demonstrates blurring the reflection generated by the JavaFX Reflection effect.
import javafx.application.Application;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.Label;
import javafx.scene.effect.*;
import javafx.scene.image.*;
import javafx.scene.layout.*;
import javafx.stage.Stage;
public class BlurredReflection extends Application {
@Override public void start(Stage stage) {
// construct a reflected label to be displayed in the scene.
Label label = new Label("The mirror crack'd from side to side");
label.setStyle("-fx-font-size: 40px; -fx-font-style: italic;");
label.setEffect(new Reflection());
// show the reflected label on the stage
StackPane layout = new StackPane();
layout.getChildren().setAll(label);
stage.setScene(new Scene(layout, 800, 200));
stage.show();
// snapshot the reflected part of the label to an image.
SnapshotParameters params = new SnapshotParameters();
Bounds effectiveBounds = label.getBoundsInParent();
params.setViewport(new Rectangle2D(effectiveBounds.getMinX(), effectiveBounds.getMinY() + effectiveBounds.getHeight() * 2 / 3, effectiveBounds.getWidth(), effectiveBounds.getHeight() * 1 /3));
WritableImage image = label.snapshot(params, null);
// blur the reflected part of the label.
ImageView reflectedView = new ImageView(image);
reflectedView.setEffect(new BoxBlur());
// place the original label (now without it's original reflection)
// in a VBox together with it's blurred reflection.
VBox blurReflectedLabel = new VBox();
label.setEffect(null);
blurReflectedLabel.getChildren().setAll(label, reflectedView);
blurReflectedLabel.setMinSize(Label.USE_PREF_SIZE, Label.USE_PREF_SIZE);
blurReflectedLabel.setPrefSize(effectiveBounds.getWidth(), effectiveBounds.getHeight());
blurReflectedLabel.setMaxSize(Label.USE_PREF_SIZE, Label.USE_PREF_SIZE);
// replace the original reflected label with the blurred reflected version.
layout.getChildren().setAll(blurReflectedLabel);
}
public static void main(String[] args) { launch(args); }
}
@jewelsea
Copy link
Author

Answer to the Oracle forum question: Effects on effect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment