2 Replies Latest reply: Jan 2, 2013 5:38 AM by kapac RSS

    animation problem

    kapac
      Hi everyone good year;
      i am trying to make an animation with this code but when i stop the animation the circle keeps its running value and don't return to its beginning value and when i play it again it plays from the running value and not the beginning value .how to return to the beginning value at each play? here is the code
      import javafx.animation.KeyFrame;
      import javafx.animation.KeyValue;
      import javafx.animation.Timeline;
      import javafx.application.Application;
      import javafx.application.Platform;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.layout.AnchorPane;
      import javafx.scene.paint.Color;
      import javafx.scene.shape.Circle;
      import javafx.stage.Stage;
      import javafx.stage.WindowEvent;
      import javafx.util.Duration;
      
      /**
       *
       * @author pa
       */
      public class Anim3 extends Application {
          Button but;
          AnchorPane ap;
          KeyValue kvx,kvy;
           KeyFrame kf;
          Circle circ;
           double r=10;
           Boolean boolplay=true;
           Timeline tl=new Timeline();
            @Override
          public void start(Stage primaryStage) {
            ap=new AnchorPane();
            drawshape(r);
            tl.setCycleCount(Timeline.INDEFINITE);
             tl.setAutoReverse(true);
              kvx=new KeyValue(circ.scaleXProperty(),15);
            kvy=new KeyValue(circ.scaleYProperty(),15);       
            ap.setStyle("-fx-background-color:yellow");
            
             but=new Button("Play");
             but.setLayoutX(10);
             but.setLayoutY(10);
              ap.getChildren().addAll(but,circ);
              but.setOnAction(new EventHandler<ActionEvent>() {
                  @Override
                  public void handle(ActionEvent t) {
                        if(boolplay){
                    tl.play();
                    boolplay=false;
                  
                      }else{               
                       tl.stop();                
                       boolplay=true;
                      
                   }
                  }
              });
               kf=new KeyFrame(Duration.millis(5000),kvx,kvy);
                tl.getKeyFrames().add(kf);
               Scene scene = new Scene(ap, 400, 400);
              
              primaryStage.setTitle("Hello World!");
              primaryStage.setScene(scene);
              primaryStage.show();
              
              
                primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>(){
                  @Override
                  public void handle(WindowEvent t) {
                      Platform.exit();
                     System.exit(0);
                  }          
             });
                
          }
      public void drawshape(double r){
          
          circ=new Circle(200,200,r);
          circ.setFill(Color.GREEN);
       }
             public static void main(String[] args) {
              launch(args);
          }
      }
       
        • 1. Re: animation problem
          James_D
          If you want to start the animation from the beginning each time, define a KeyFrame for the start of the animation:
          KeyFrame startKF = new KeyFrame(Duration.ZERO, new KeyValue(circ.scaleXProperty(), 1), new KeyValue(circ.scaleYProperty(), 1));
          ...
          // instead of t1.getKeyFrames().add(kf):
          tl.getKeyFrames().addAll(startKF, kf);
          If you want the animation to continue from where it left off, but (unlike your current version) animate to the full range of scale (from 1 to 15), you can call tl.pause() instead of tl.stop().

          If you want the circle to revert to its original size when the animation is stopped, just call
          circ.setScaleX(1);
          circ.setScaleY(1);
          when you call stop().

          Two other things:
          You might prefer to use a ScaleTransition instead of a Timeline. It means you don't have to clutter your code with KeyValues and KeyFrames.
          Your boolplay variable is a bit redundant; you can check the currentRate property of the animation (timeline or scaleTransition). It will be 0.0 if the animation is not running, and non-zero if it is running.

          Edited by: James_D on Jan 1, 2013 12:51 PM
          • 2. Re: animation problem
            kapac
            Hi;
            Thanks a lot
            it's quite clear now
            regards