Legal Terms and Copyright Notice
/* 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * Copyright © 2010, Oracle and/or its affiliates. All rights reserved.
 * Oracle is a registered trademark of Oracle Corporation and/or its affiliates. 
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
 * Other names may be trademarks of their respective owners.
 * 
 * This file is available and licensed under the following license:
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:

 *   *  Redistributions of source code must retain the above copyright notice, 
        trademark notice, this list of conditions, and the following disclaimer.

 *   *  Redistributions in binary form must reproduce the above copyright notice, 
        trademark notice, this list of conditions, and the following disclaimer in 
        the documentation and/or other materials provided with the distribution.

 *   *  Neither the name of Oracle nor the names of its contributors may be used 
        to endorse or promote products derived from this software without specific 
        prior written permission.
 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package animatedmotion;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.CustomNode;
import javafx.scene.input.MouseEvent;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.shape.ArcTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.animation.Timeline;
import javafx.animation.transition.FadeTransition;
import javafx.animation.transition.ScaleTransition;
import javafx.animation.transition.Transition;
import javafx.animation.transition.RotateTransition;
import javafx.animation.transition.TranslateTransition;
import javafx.animation.Interpolator;
import javafx.animation.transition.PathTransition;
import javafx.animation.transition.OrientationType;
import javafx.animation.transition.AnimationPath;


def cloudImage = Image {url: "{__DIR__}cloud.png"}
def boatImage = Image {url: "{__DIR__}boat.png"}
def bubbleImage = Image {url: "{__DIR__}bubble.png"}
def fishImage = Image {url: "{__DIR__}fish.png"}
def baloonImage = Image {url: "{__DIR__}baloon.png"}
def backgroundImage = Image {url: "{__DIR__}bg.png"}


class AnimatedImage extends CustomNode {

  var image: Image;
  var transition: Transition;
  var fitWidth: Number;
  override function create() {
    transition.node = ImageView {
           fitWidth: fitWidth
           preserveRatio: true
           image: image
           onMouseClicked: function( e: MouseEvent ):Void {
            if (not transition.running or transition.paused) {
             transition.play()
             } else {
             transition.pause()
             }
           }
    }
  }
}

//Fade transition
var cloud = AnimatedImage {
           image: cloudImage
           translateX: 10
           translateY: 10
           transition: FadeTransition {
                 duration: 3s
                 toValue: 0.3
                 repeatCount: Timeline.INDEFINITE autoReverse: true
           }
}

//Path transition
var path = Path {
        elements: [
            MoveTo { x: 50 y: 50 },
            ArcTo { x: 380  y: 50  radiusX: 350  radiusY: 80 sweepFlag: true}
        ]
};

var boat = AnimatedImage {
           image: boatImage
           translateY: 330
           translateX: 40
           fitWidth: 100
           transition: PathTransition {
                 duration: 5s
                 path: AnimationPath.createFromPath(path)
                 orientation: OrientationType.ORTHOGONAL_TO_TANGENT
                 repeatCount: Timeline.INDEFINITE autoReverse: true
           }
}

//Rotate transition
var bubbles = AnimatedImage {
           image: bubbleImage
           translateX: 280
           translateY: 450
           fitWidth: 30
           transition: RotateTransition {
                    duration: 3s
                    byAngle: 360 repeatCount:Timeline.INDEFINITE
                    interpolator: Interpolator.LINEAR
           }
}


//Scale transition
var fish = AnimatedImage {
           image: fishImage
           translateX: 300
           translateY: 450
           fitWidth: 100
           transition: ScaleTransition {
                 duration: 1s
                 toX: 0.8 toY: 0.8
                 repeatCount:Timeline.INDEFINITE autoReverse: true
           }
}

// Translate transition
var baloon = AnimatedImage {
           image: baloonImage
           translateX: 300
           translateY: 100
           fitWidth: 150
           transition: TranslateTransition {
                  duration: 2s
                  byY: 100 repeatCount: Timeline.INDEFINITE
                  autoReverse: true
           }
}

var background = ImageView {image: backgroundImage}

Stage {
    title: "Animated Transitions"
    width: 580
    height: 700
    scene: Scene {
        content: [
          background,
          cloud,
          baloon,
          boat,
          fish,
          bubbles        
        ]
    }
}