1  import javafx.animation.FadeTransition;
  2  import javafx.animation.Timeline;
  3  import javafx.application.Application;
  4  import javafx.scene.Scene;
  5  import javafx.scene.layout.Pane;
  6  import javafx.scene.paint.Color;
  7  import javafx.scene.shape.Ellipse;
  8  import javafx.stage.Stage;
  9  import javafx.util.Duration;
 10  
 11  public class FadeTransitionDemo extends Application {
 12    @Override // Override the start method in the Application class
 13    public void start(Stage primaryStage) {
 14      // Place an ellipse to the pane
 15      Pane pane = new Pane();
 16      Ellipse ellipse = new Ellipse(10, 10, 100, 50);
 17      ellipse.setFill(Color.RED); 
 18      ellipse.setStroke(Color.BLACK);
 19      ellipse.centerXProperty().bind(pane.widthProperty().divide(2));
 20      ellipse.centerYProperty().bind(pane.heightProperty().divide(2));    
 21      ellipse.radiusXProperty().bind(
 22        pane.widthProperty().multiply(0.4));    
 23      ellipse.radiusYProperty().bind(
 24        pane.heightProperty().multiply(0.4)); 
 25      pane.getChildren().add(ellipse);
 26      
 27      // Apply a fade transition to ellipse
 28      FadeTransition ft = 
 29        new FadeTransition(Duration.millis(3000), ellipse);
 30      ft.setFromValue(1.0);
 31      ft.setToValue(0.1);
 32      ft.setCycleCount(Timeline.INDEFINITE);
 33      ft.setAutoReverse(true);
 34      ft.play(); // Start animation 
 35      
 36      // Control animation
 37      ellipse.setOnMousePressed(e -> ft.pause());
 38      ellipse.setOnMouseReleased(e -> ft.play());
 39      
 40      // Create a scene and place it in the stage
 41      Scene scene = new Scene(pane, 200, 150);
 42      primaryStage.setTitle("FadeTransitionDemo"); // Set the stage title
 43      primaryStage.setScene(scene); // Place the scene in the stage
 44      primaryStage.show(); // Display the stage
 45    }
 46    
 47    /**
 48     * The main method is only needed for the IDE with limited
 49     * JavaFX support. Not needed for running from the command line.
 50     */
 51    public static void main(String[] args) {
 52      launch(args);
 53    }
 54  }