Gradient Paint


Besides applying simple colors to visual objects, JavaFX provides built-in classes to apply gradient effect to objects.  Gradients are created using a gradient class such as javafx.scene.paint.LinearGradient and the javafx.scene.paint.Stop class to define a graded color that makes up the gradient.  The following creates a graded Rectangle: 

def w = 400;
def h = 200;

def linearGrad = LinearGradient {
    startX: 0.0, startY: 0.0, endX: 0.0, endY: 1.0
    proportional: true
    stops: [ 
        Stop {offset: 0.0 color: Color.RED},
        Stop {offset: 1.0 color: Color.BLACK},
    ]
}
def rec0 = Rectangle {
    width: 100 height: 90
    x: 30 y: h - 90
    fill: linearGrad
    stroke:Color.SILVER
}

The code above produces the figure shown below.  Notice the two colors, Color.BLACK and Color.RED, declared in the two instances of Stop.




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!