Skip to content

Instantly share code, notes, and snippets.

@skrb
Created December 19, 2012 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skrb/4335101 to your computer and use it in GitHub Desktop.
Save skrb/4335101 to your computer and use it in GitHub Desktop.
Example of JavaFX binding.
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Line;
import javafx.stage.Stage;
class Ball extends Circle {
private double dragBaseX;
private double dragBaseY;
public Ball(double centerX, double centerY, double radius) {
super(centerX, centerY, radius);
setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
dragBaseX = event.getSceneX() - getCenterX();
dragBaseY = event.getSceneY() - getCenterY();
}
});
setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
setCenterX(event.getSceneX() - dragBaseX);
setCenterY(event.getSceneY() - dragBaseY);
}
});
}
}
class Connection extends Line {
public Connection(Ball startBall, Ball endBall) {
startXProperty().bind(startBall.centerXProperty());
startYProperty().bind(startBall.centerYProperty());
endXProperty().bind(endBall.centerXProperty());
endYProperty().bind(endBall.centerYProperty());
}
}
public class ConnectedBall extends Application {
@Override
public void start(Stage stage) throws Exception {
Group root = new Group();
initShapes(root);
Scene scene = new Scene(root, 400, 400);
stage.setScene(scene);
stage.show();
}
private void initShapes(Group root) {
Ball ball1 = new Ball(100, 200, 20);
ball1.setFill(Color.RED);
root.getChildren().add(ball1);
Ball ball2 = new Ball(300, 200, 20);
ball2.setFill(Color.RED);
root.getChildren().add(ball2);
Connection connection = new Connection(ball1, ball2);
connection.setStroke(Color.CYAN);
connection.setStrokeWidth(5);
root.getChildren().add(0, connection);
}
public static void main(String... args) {
launch(args);
}
}
@Bharatshah
Copy link

nice example to understand concept

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment