Animation‎ > ‎

Animate Multiple Objects


The JavaFX Transition API lets you quickly create animation sequences based on object properties.  Each Transition class (see others) only animates one object at a time.  To be able to create compound animation sequences involving several subjects, you must use ParallelTransition class.  This class facilitates the creation of animation sequences composed of other transition classes.  The code shown below uses one RotationTransition and two TranslationTransition to create animate two objects in paramellel:. 

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

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

    def rect = Rectangle {x:10 y:10 width:50 height:20 fill:Color.RED}

    ParallelTransition {
        content: [
            RotateTransition {
                node: circles
                duration: 1s
                byAngle:360
                repeatCount: FadeTransition.INDEFINITE autoReverse: true
            }

            TranslateTransition {
                node: circles
                duration: 3s
                byX:w/2
                repeatCount: FadeTransition.INDEFINITE autoReverse: true
            }

            TranslateTransition {
                node: rect
                duration: 3s
                byY:h-30
                repeatCount: FadeTransition.INDEFINITE autoReverse: true
            }
        ]
    }.play();

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!