Animation‎ > ‎

Animate Rotation


The JavaFX Transition API lets you quickly create animation sequences based on object properties.  The following code shows how to animate an object's x/y-rotation in space using the RotateTransition. The code groups two Circle shapes together using the Group class.  Then, animates the group using the RotateTransition class (see result below). 

def w = 400;
def h = 200;
def r = 50;

def circles = Group{
    cache: true
    content:[
        Circle {
            centerX: (w / 2) - r
            centerY: (h / 2)
            radius: r
            fill: Color.BLUE;
            stroke: Color.WHITE;
            strokeWidth: 3
        }

        Circle {
            centerX: (w / 2) + r
            centerY: (h / 2)
            radius: r
            fill: Color.BLUE;
            stroke: Color.WHITE;
            strokeWidth: 3
        }
    ]
}

RotateTransition {
    node: circles
    duration: 1s
    byAngle:360
    repeatCount: RotateTransition.INDEFINITE autoReverse: true
}.playFromStart();

See Animation
When the code execute, you will see the circles rotate about the center of their junction.  
Click here to see animation.



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!