Property updates

Pavel Safrata pavel.safrata at oracle.com
Mon Jun 11 08:30:47 PDT 2012


Hi,

On 11.6.2012 16:52, goddard at seznam.cz wrote:
> Hi,
>
> thanks for the answer. I've thought that my code does this:
> - create circle at random coords
> - create random end coords
> - create animation with all these coords
> - call setOnFinished every time when animation ends
> - inside handle, do following:
> -- assign the current end coords to the target coords, so the next 
> animation starts at the end
> -- generate new end coords, so the next animation ends at different 
> coords

Here is your problem. You generate new coords, but you store them to the 
randCoords array, which has no influence on the timeline. During 
creation of the timeline, you call

new KeyValue(c.centerXProperty(), randCoords[0].get())

In the above call, randCoords[0].get() is called, returns a number (the 
first generated random end coordinate), and this number is passed to 
KeyValue's constructor. So the KeyValue then always moves 
centerXProperty to the point specified by the number and has no 
reference to the randCoords array you later keep updating.

> -- play next animation
>
> From the point where I'm callung the setOnFinished, it should be like 
> a never-ending loop. Accordning to the println output, I'm updating 
> the end coords, but the circle coords are not updated even I'm calling 
> set() on the cX/Y property.

I can see the printline is above the setters. It shows the coordinates 
of the end of the animation, which are always the same. If you move the 
printline below the setters, it will certainly show the new values.

> I'm not sure what I should change in the code so it works properly.

For moving nodes you should generally use transitions, but if this is a 
simplification of some more complex code, you need to update the 
timeline's key frame each time you are starting new round.

I hope I've made it more clear this time..
With regards,
Pavel

>
> Apologies, Jiri
>
> ------------ Původní zpráva ------------
> Od: Pavel Safrata <pavel.safrata at oracle.com>
> Předmět: Re: Property updates
> Datum: 11.6.2012 13:48:14
> ----------------------------------------
> 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