Combining effects

By combining just reflection and shadow effects, you can already achieve nice-looking elements, for example:

Refer to the following code snippet:

// chapter8/combining/ReflectionAndShadows.java
public void start(Stage stage) {
// outer rectangle has a inner shadow
Rectangle rect = new Rectangle(120, 120);
rect.setFill(Color.WHITE);
rect.setStroke(Color.BLACK);
rect.setStrokeWidth(4);
rect.setEffect(new InnerShadow(15, Color.BLACK));

// text has a drop shadow
Text text = new Text("FX");
text.setFont(Font.font("Arial", FontWeight.BOLD, 80));
text.setEffect(new DropShadow());

// to apply reflection to both
// we need to have a common node
StackPane square = new StackPane(rect, text);
// this way our StackPane will be small
// and just wrap the rectangle
square.setMaxSize(0, 0);

// aplying reflection
Reflection reflection = new Reflection();
reflection.setFraction(0.45);
square.setEffect(reflection);

StackPane root = new StackPane(square);
stage.setTitle("Effects Demo");
stage.setScene(new Scene(root, 300, 250));
stage.show();
}

Another option to combine effects is setting one effect as an input for another with the setInput() method:

public void start(Stage primaryStage) {
Rectangle rect1 = new Rectangle(50, 50, Color.RED);
DropShadow effect1 = new DropShadow();
effect1.setInput(new Reflection());
rect1.setEffect(effect1);

Rectangle rect2 = new Rectangle(50, 50, Color.RED);
Reflection effect2 = new Reflection();
effect2.setInput(new DropShadow());
rect2.setEffect(effect2);

HBox root = new HBox(30, rect1, rect2);
root.setPadding(new Insets(50));
primaryStage.setTitle("Inputs");
primaryStage.setScene(new Scene(root, 230, 200));
primaryStage.show();
}

You can chain any number of effects this way. But you need to be careful about the order. The preceding example will produce two different outputs for the same effects applied in a different order:

The thing is that each effect changes the actual size of the Node (boundsInParent) and the next one will use this new size and produce a different result.

Another option for combining effects is blending, which we will review in the Using blend effects section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.218.221