Tuesday, June 21, 2016

58. Reflection

The Reflection effect can be used on a node. The offset will indicate the distance downwards where the reflection starts. Here we only applied to 90% of the height, and had top opacity as .8 and lower as 0.4 (much lighter).


We can see the effect on an ImageView node and a text node.


package ex58;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.effect.Reflection;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Ex58 extends Application {
    
    @Override
    public void start(Stage stage) {
        
        HBox root = new HBox(50);
        
        Reflection reflection = new Reflection();
        reflection.setFraction(.9);
        reflection.setTopOffset(5);
        reflection.setTopOpacity(.8);
        reflection.setBottomOpacity(.4);
        
        String hillary = "Hillary";
        String clinton = "Clinton";
        
        Image image=new Image(hillary+clinton+".png",200,200,true,true);
        ImageView imageView = new ImageView(image);
        imageView.setEffect(reflection);
        
        Text text = new Text(100, 100, hillary + " " + clinton);
        text.setFont(Font.font("Georgia", FontWeight.BOLD, 
                FontPosture.ITALIC, 60));
        text.setFill(Color.BLUE);
        text.setEffect(reflection);
        
        root.getChildren().addAll(imageView, text);
        
        Scene scene = new Scene(root, 700, 400, Color.LIGHTYELLOW);
        stage.setTitle("Example 58. Reflection");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        Application.launch(args);
    }
}

This is the output:


No comments:

Post a Comment