Reflection on Objects


A fun and popular effect that can be applied to any visual object in a JavaFX scene graph is Reflection found in package javafx.scene.effect.  The following shows how to apply a Reflection instance to a Rectangle:

var grad =  LinearGradient {
    startX: 0.0, startY: 0.0, endX: 0.0, endY: 1.0
    proportional: true
    stops: [ Stop { offset: 0.0 color: Color.DARKBLUE },
             Stop { offset: 1.0 color: Color.LIGHTBLUE } ]
}

var reflect = Reflection {
    fraction: 0.50
    topOpacity: 0.50
    bottomOpacity: 0.0
    topOffset: 2.0
};

var rect = Rectangle {
    x:130 y:50 width:100, height:80
    stroke:Color.RED strokeWidth:2
    arcHeight:10 arcWidth:10
    fill:grad
    effect:reflect
}

The code above creates a rectangle shape with a reflection as shown below.  Note that you can use the properties on the Reflection instance to control the percentage of the object is reflected.


The materials on this website represent a small sample of content loosely based on the book JavaFX Application Development Cookbook.  The book offers far more content with over 80 recipes covering a range of topics from basics to advanced concepts.  

Buy the Book

You can get your copy of the book directly from the publisher. Click here to order!