JavaFX TranslateTransition create

Description

JavaFX TranslateTransition create

import javafx.animation.Interpolator;
import javafx.animation.TranslateTransition;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Main extends Application {
   public static void main(String[] args) {
      Application.launch(args);/*from  w ww  . j a  v a2s  . c  o m*/
   }

   @Override
   public void start(Stage primaryStage) {
      primaryStage.setTitle("java2s.com");
      Group root = new Group();
      final Scene scene = new Scene(root, 300, 250);

      // create ticker area
      final Group tickerArea = new Group();
      final Rectangle tickerRect = new Rectangle();
      tickerRect.setX(0);
      tickerRect.setY(0);
      tickerRect.setWidth(scene.getWidth() - 6);
      tickerRect.setHeight(30);
      tickerRect.setStroke(Color.rgb(255, 255, 255, .70));
      Rectangle clipRegion = new Rectangle();
      clipRegion.setX(0);
      clipRegion.setY(0);
      clipRegion.setWidth(scene.getWidth() - 6);
      clipRegion.setHeight(30);

      tickerArea.setClip(clipRegion);

      // Resize the ticker area when the window is resized
      tickerArea.setTranslateX(6);
      tickerArea.translateYProperty().bind(scene.heightProperty().subtract(tickerRect.getHeight() + 6));
      tickerRect.widthProperty().bind(scene.widthProperty().subtract(36));
      clipRegion.widthProperty().bind(scene.widthProperty().subtract(10));
      tickerArea.getChildren().add(tickerRect);
      root.getChildren().add(tickerArea); // add news text
      Text news = new Text();
      news.setText("test");
      news.setTranslateY(18);
      news.setFill(Color.WHITE);
      tickerArea.getChildren().add(news);
      
      final TranslateTransition ticker = new TranslateTransition();
      ticker.setNode(news);
      int newsLength = 300;
      ticker.setDuration(Duration.millis((newsLength * 4 / 300) * 15000));
      ticker.setFromX(scene.widthProperty().doubleValue());
      ticker.setToX(-scene.widthProperty().doubleValue() - (newsLength * 5));
      ticker.setFromY(19);
      ticker.setInterpolator(Interpolator.LINEAR);
      ticker.setCycleCount(1);

      // when ticker has finished reset and replay ticker animation
      ticker.setOnFinished((ActionEvent ae) -> {
         ticker.stop();
         ticker.setFromX(scene.getWidth());
         ticker.setDuration(new Duration((newsLength * 4 / 300) * 15000));
         ticker.playFromStart();
      });

      tickerArea.setOnMouseEntered((MouseEvent me) -> {
         ticker.pause();
      });

      tickerArea.setOnMouseExited((MouseEvent me) -> {
         ticker.play();
      });

      ticker.play();

      primaryStage.setScene(scene);
      primaryStage.show();
   }

}



PreviousNext

Related