lundi 16 mars 2015

Binding the line to the centre of a node


Vote count:

0




I am creating an animation, where I have two circles joined by a line. When the nodes(circles) move, I want the line to bind their centres. What I have tried?



import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
import javafx.util.Duration;

public class BindIt extends Application {

@Override
public void start(Stage primaryStage) {
DoubleProperty startX = new SimpleDoubleProperty(100);
DoubleProperty startY = new SimpleDoubleProperty(100);
DoubleProperty endX = new SimpleDoubleProperty(300);
DoubleProperty endY = new SimpleDoubleProperty(300);
Line line = new Line(100, 100, 300, 300);
line.startXProperty().bind(startX);
line.startYProperty().bind(startY);
line.endXProperty().bind(endX);
line.endYProperty().bind(endY);
Circle c1 = new Circle(25);
c1.setCenterX(100);
c1.setCenterY(100);
c1.centerXProperty().bind(startX);
c1.centerYProperty().bind(startY);

Circle c2 = new Circle(25);
c2.setCenterX(300);
c2.setCenterY(300);
c2.centerXProperty().bind(endX);
c2.centerYProperty().bind(endY);


Group root = new Group();
root.getChildren().add(line);
root.getChildren().add(c1);
root.getChildren().add(c2);

Scene scene = new Scene(root, 500, 500);

primaryStage.setTitle("Bind the line!");
primaryStage.setScene(scene);
final Timeline timeline = new Timeline();
timeline.getKeyFrames().addAll(new KeyFrame(Duration.millis(0)), new KeyFrame(Duration.millis(2000), new KeyValue(c1.centerXProperty(), 200)));
timeline.play();

primaryStage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

}


Can someone tell me where am I doing wrong?



asked 39 secs ago







Binding the line to the centre of a node

Aucun commentaire:

Enregistrer un commentaire