Property updates

Pavel Safrata pavel.safrata at oracle.com
Mon Jun 11 04:46:51 PDT 2012


Hello Jiri,
the coordinates are updated during the animation, but always finish on 
the same value. It's because you create the timeline passing the current 
values of the x and y properties and then run it over and over without 
updating it, so it always finishes with the circle on the same 
coordinates (the first random values generated for x an y). The pause is 
caused by the fact that your code makes the second run of the timeline 
begin on the same place where it ends (so the animation runs and moves 
the circle about zero pixels).
With regards,
Pavel

On 11.6.2012 12:48, goddard at seznam.cz wrote:
> This is the output from the println statements:
> centerXDoubleProperty [bean: Circle at 157a5c3, name: centerX, value: 396.34924225837125]
> x: DoubleProperty [value: 396.34924225837125]
> centerXDoubleProperty [bean: Circle at 157a5c3, name: centerX, value: 396.34924225837125]
> x: DoubleProperty [value: 563.9584002793741]
> centerXDoubleProperty [bean: Circle at 157a5c3, name: centerX, value: 396.34924225837125]
> x: DoubleProperty [value: 337.0834495091644]
>
> As you can see, the centerX value doesn't change at all. Any help is appreciated.
>
> Regards, jiri
>
> ------------ Původní zpráva ------------
> Od:<goddard at seznam.cz>
> Předmět: Property updates
> Datum: 10.6.2012 22:54:59
> ----------------------------------------
> Hello,
>
> the following code should create a Circle at random coords and animate the
> translation to random coords. The point is to randomly translate the circle from
> one location to another successively:
>
> package boyle;
>
> import javafx.animation.KeyFrame;
> import javafx.animation.KeyValue;
> import javafx.animation.Timeline;
> import javafx.animation.TimelineBuilder;
> import javafx.application.Application;
> import javafx.beans.property.DoubleProperty;
> import javafx.beans.property.SimpleDoubleProperty;
> import javafx.event.ActionEvent;
> import javafx.event.EventHandler;
> import javafx.scene.Group;
> import javafx.scene.Scene;
> import javafx.scene.paint.Color;
> import javafx.scene.shape.Circle;
> import javafx.scene.shape.CircleBuilder;
> import javafx.stage.Stage;
> import javafx.util.Duration;
>
> /**
>   *
>   * @author jiri
>   */
> public class Boyle extends Application {
>
>      public static final int WIDTH = 800;
>      public static final int HEIGHT = 512;
>
>      /**
>       * @param args the command line arguments
>       */
>      public static void main(String[] args) {
>          launch(args);
>      }
>
>      // end the animation at random coords
>      private DoubleProperty x = new SimpleDoubleProperty(Math.random() * WIDTH);
>      private DoubleProperty y = new SimpleDoubleProperty(Math.random() *
> HEIGHT);
>
>      @Override public void start(Stage primaryStage) {
>          final Circle c = CircleBuilder.create()
>                  // start the animation at random coords
>                  .centerX(Math.random() * WIDTH)
>                  .centerY(Math.random() * HEIGHT)
>                  .radius(15).fill(Color.BLACK)
>                  .build();
>
>          final DoubleProperty[] randCoords = {x, y};
>
>          final Timeline t = TimelineBuilder.create()
>                  .keyFrames(new KeyFrame(Duration.seconds(5),
>                  new KeyValue(c.centerXProperty(), randCoords[0].get()),
>                  new KeyValue(c.centerYProperty(), randCoords[1].get())))
>                  .build();
>
>          // refreshes coords at every end of the animation
>          t.setOnFinished(new EventHandler<ActionEvent>() {
>
>              @Override
>              public void handle(ActionEvent ae) {
>                  System.out.println("centerX"+ c.centerXProperty());
>                  // set animation end coords to the target
>                  c.centerXProperty().set(randCoords[0].get());
>                  c.centerYProperty().set(randCoords[1].get());
>                  System.out.println("x: "+ randCoords[0]);
>                  // generate new coords for the animation end
>                  randCoords[0].set(Math.random() * WIDTH);
>                  randCoords[1].set(Math.random() * HEIGHT);
>                  t.play();
>              }
>          });
>
>          t.play();
>
>          Group root = new Group();
>          root.getChildren().add(c);
>
>          Scene scene = new Scene(root, WIDTH, HEIGHT);
>
>          primaryStage.setTitle("@javafxcz - Boyle - www.dredwerkz.cz");
>          primaryStage.setScene(scene);
>          primaryStage.show();
>      }
> }
>
> However, the circle appears moving to its target coords instead to its end
> coords, and update of the target coords doesn't work - unlike for the end
> coords. Plus, there's a significant 1-2 seconds long pause before the animation
> starts again in the handler, but just for the first time. Then it runs
> smoothly.
> Help in form of explanation / code sample is appreciated.
>
> Regards, Jiri
>
>


More information about the openjfx-dev mailing list