Advertisement
iamaamir

ShakeME

Jun 12th, 2015
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1.  
  2. import javafx.animation.Interpolator;
  3. import javafx.animation.KeyFrame;
  4. import javafx.animation.KeyValue;
  5. import javafx.animation.Timeline;
  6. import javafx.application.Application;
  7. import javafx.scene.CacheHint;
  8. import javafx.scene.Scene;
  9. import javafx.scene.control.Button;
  10. import javafx.scene.layout.StackPane;
  11. import javafx.stage.Stage;
  12. import javafx.util.Duration;
  13.  
  14. /**
  15.  *
  16.  * @author toffee boy Aamir
  17.  */
  18. public class ShakeMe extends Application {
  19.  
  20.     private final Interpolator WEB_EASE = Interpolator.SPLINE(0.25, 0.1, 0.25, 1);
  21.  
  22.     @Override
  23.     public void start(Stage primaryStage) {
  24.  
  25.         Button node = new Button();
  26.         node.setCache(true);
  27.         node.setCacheHint(CacheHint.QUALITY);
  28.         Timeline anim = new Timeline(
  29.                 new KeyFrame(Duration.millis(0), new KeyValue(node.translateXProperty(), 0, WEB_EASE)),
  30.                 new KeyFrame(Duration.millis(100), new KeyValue(node.translateXProperty(), -10, WEB_EASE)),
  31.                 new KeyFrame(Duration.millis(200), new KeyValue(node.translateXProperty(), 10, WEB_EASE)),
  32.                 new KeyFrame(Duration.millis(300), new KeyValue(node.translateXProperty(), -10, WEB_EASE)),
  33.                 new KeyFrame(Duration.millis(400), new KeyValue(node.translateXProperty(), 10, WEB_EASE)),
  34.                 new KeyFrame(Duration.millis(500), new KeyValue(node.translateXProperty(), -10, WEB_EASE)),
  35.                 new KeyFrame(Duration.millis(600), new KeyValue(node.translateXProperty(), 10, WEB_EASE)),
  36.                 new KeyFrame(Duration.millis(700), new KeyValue(node.translateXProperty(), -10, WEB_EASE)),
  37.                 new KeyFrame(Duration.millis(800), new KeyValue(node.translateXProperty(), 10, WEB_EASE)),
  38.                 new KeyFrame(Duration.millis(900), new KeyValue(node.translateXProperty(), -10, WEB_EASE)),
  39.                 new KeyFrame(Duration.millis(1000), new KeyValue(node.translateXProperty(), 0, WEB_EASE))
  40.         );
  41.  
  42.         anim.setDelay(Duration.seconds(0.2));
  43.         node.setText("Click Me");
  44.         node.setOnAction(e -> {
  45.             node.setCacheHint(CacheHint.SPEED);
  46.             anim.playFromStart();
  47.  
  48.         });
  49.  
  50.         StackPane root = new StackPane();
  51.         root.getChildren().add(node);
  52.  
  53.         Scene scene = new Scene(root, 300, 250);
  54.  
  55.         primaryStage.setTitle("Shake me");
  56.         primaryStage.setScene(scene);
  57.         primaryStage.show();
  58.     }
  59.  
  60.     /**
  61.      * @param args the command line arguments
  62.      */
  63.     public static void main(String[] args) {
  64.         launch(args);
  65.     }
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement