Add a node in the middle of a line java fx












0















I have a line and I want a square with text inside of the square to be placed in the middle of this line.



I have created the square with text using a stack pane. This line is draggable so I want the square to stay in the middle of this line when it is being dragged.



I tried:



weightSquare.layoutXProperty().bind((line.startXProperty().add(line.endXProperty())).divide(2).add(line.translateXProperty()));
weightSquare.layoutYProperty().bind((line.startYProperty().add(line.endYProperty())).divide(2).add(line.translateXProperty()));


where weightSquare is a StackPane containing a rectangle and text.



Currently, the weightSquare is near the middle of the line but not perfectly in the middle. When the line moves around the weightSquare stays relatively near the middle of the line but sometimes goes off the line slightly.



I want something like this:



Example of what I want



Thank you.










share|improve this question



























    0















    I have a line and I want a square with text inside of the square to be placed in the middle of this line.



    I have created the square with text using a stack pane. This line is draggable so I want the square to stay in the middle of this line when it is being dragged.



    I tried:



    weightSquare.layoutXProperty().bind((line.startXProperty().add(line.endXProperty())).divide(2).add(line.translateXProperty()));
    weightSquare.layoutYProperty().bind((line.startYProperty().add(line.endYProperty())).divide(2).add(line.translateXProperty()));


    where weightSquare is a StackPane containing a rectangle and text.



    Currently, the weightSquare is near the middle of the line but not perfectly in the middle. When the line moves around the weightSquare stays relatively near the middle of the line but sometimes goes off the line slightly.



    I want something like this:



    Example of what I want



    Thank you.










    share|improve this question

























      0












      0








      0








      I have a line and I want a square with text inside of the square to be placed in the middle of this line.



      I have created the square with text using a stack pane. This line is draggable so I want the square to stay in the middle of this line when it is being dragged.



      I tried:



      weightSquare.layoutXProperty().bind((line.startXProperty().add(line.endXProperty())).divide(2).add(line.translateXProperty()));
      weightSquare.layoutYProperty().bind((line.startYProperty().add(line.endYProperty())).divide(2).add(line.translateXProperty()));


      where weightSquare is a StackPane containing a rectangle and text.



      Currently, the weightSquare is near the middle of the line but not perfectly in the middle. When the line moves around the weightSquare stays relatively near the middle of the line but sometimes goes off the line slightly.



      I want something like this:



      Example of what I want



      Thank you.










      share|improve this question














      I have a line and I want a square with text inside of the square to be placed in the middle of this line.



      I have created the square with text using a stack pane. This line is draggable so I want the square to stay in the middle of this line when it is being dragged.



      I tried:



      weightSquare.layoutXProperty().bind((line.startXProperty().add(line.endXProperty())).divide(2).add(line.translateXProperty()));
      weightSquare.layoutYProperty().bind((line.startYProperty().add(line.endYProperty())).divide(2).add(line.translateXProperty()));


      where weightSquare is a StackPane containing a rectangle and text.



      Currently, the weightSquare is near the middle of the line but not perfectly in the middle. When the line moves around the weightSquare stays relatively near the middle of the line but sometimes goes off the line slightly.



      I want something like this:



      Example of what I want



      Thank you.







      javafx






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 20:45









      JamanJaman

      486




      486
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Assuming no transformations have been applied to the line or the StackPane, you can calculate the position of the StackPane based on the line properties like this



          stackPane.layoutX = (line.startX + line.endX - stackPane.width) / 2;


          (Procede accordingly for y coordinates.)



          transformX and transformY could simply be added, but general transforms would require you to




          1. Listen to changes of the transforms

          2. Use localToParent on the start/end coordinates of the line to get the location in the parent.


          I recommend using Bindings.createDoubleBindings for complicate double bindings btw, since this makes the formula for calculating the values much easier to read.



          Example



          I use a Label, since this provides background/border functionality too.



          @Override
          public void start(Stage primaryStage) throws IOException {
          Label label = new Label();
          label.setStyle("-fx-background-color: white; -fx-border-color: black;");
          label.setPadding(new Insets(2, 4, 2, 4));

          Line line = new Line(300, 300, 300, 100);

          label.layoutXProperty().bind(Bindings.createDoubleBinding(
          () -> (line.getStartX() + line.getEndX() - label.getWidth()) / 2,
          line.startXProperty(), line.endXProperty(), label.widthProperty()));
          label.layoutYProperty().bind(Bindings.createDoubleBinding(
          () -> (line.getStartY() + line.getEndY() - label.getHeight()) / 2,
          line.startYProperty(), line.endYProperty(), label.heightProperty()));

          DoubleProperty angle = new SimpleDoubleProperty();
          line.endXProperty().bind(Bindings.createDoubleBinding(() -> 300 + 200 * Math.sin(angle.get()), angle));
          line.endYProperty().bind(Bindings.createDoubleBinding(() -> 300 + 200 * Math.cos(angle.get()), angle));

          Timeline timeline = new Timeline(
          new KeyFrame(Duration.ZERO, new KeyValue(angle, 0d, Interpolator.LINEAR)),
          new KeyFrame(Duration.seconds(10), new KeyValue(angle, Math.PI * 2, Interpolator.LINEAR)));
          timeline.setCycleCount(Animation.INDEFINITE);
          label.textProperty().bind(timeline.currentTimeProperty().asString());
          timeline.play();

          Scene scene = new Scene(new Pane(line, label), 600, 600);

          primaryStage.setScene(scene);
          primaryStage.show();
          }





          share|improve this answer
























          • Thanks Fabian for the answer, this was really helpful.

            – Jaman
            Nov 15 '18 at 22:43











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327646%2fadd-a-node-in-the-middle-of-a-line-java-fx%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Assuming no transformations have been applied to the line or the StackPane, you can calculate the position of the StackPane based on the line properties like this



          stackPane.layoutX = (line.startX + line.endX - stackPane.width) / 2;


          (Procede accordingly for y coordinates.)



          transformX and transformY could simply be added, but general transforms would require you to




          1. Listen to changes of the transforms

          2. Use localToParent on the start/end coordinates of the line to get the location in the parent.


          I recommend using Bindings.createDoubleBindings for complicate double bindings btw, since this makes the formula for calculating the values much easier to read.



          Example



          I use a Label, since this provides background/border functionality too.



          @Override
          public void start(Stage primaryStage) throws IOException {
          Label label = new Label();
          label.setStyle("-fx-background-color: white; -fx-border-color: black;");
          label.setPadding(new Insets(2, 4, 2, 4));

          Line line = new Line(300, 300, 300, 100);

          label.layoutXProperty().bind(Bindings.createDoubleBinding(
          () -> (line.getStartX() + line.getEndX() - label.getWidth()) / 2,
          line.startXProperty(), line.endXProperty(), label.widthProperty()));
          label.layoutYProperty().bind(Bindings.createDoubleBinding(
          () -> (line.getStartY() + line.getEndY() - label.getHeight()) / 2,
          line.startYProperty(), line.endYProperty(), label.heightProperty()));

          DoubleProperty angle = new SimpleDoubleProperty();
          line.endXProperty().bind(Bindings.createDoubleBinding(() -> 300 + 200 * Math.sin(angle.get()), angle));
          line.endYProperty().bind(Bindings.createDoubleBinding(() -> 300 + 200 * Math.cos(angle.get()), angle));

          Timeline timeline = new Timeline(
          new KeyFrame(Duration.ZERO, new KeyValue(angle, 0d, Interpolator.LINEAR)),
          new KeyFrame(Duration.seconds(10), new KeyValue(angle, Math.PI * 2, Interpolator.LINEAR)));
          timeline.setCycleCount(Animation.INDEFINITE);
          label.textProperty().bind(timeline.currentTimeProperty().asString());
          timeline.play();

          Scene scene = new Scene(new Pane(line, label), 600, 600);

          primaryStage.setScene(scene);
          primaryStage.show();
          }





          share|improve this answer
























          • Thanks Fabian for the answer, this was really helpful.

            – Jaman
            Nov 15 '18 at 22:43
















          1














          Assuming no transformations have been applied to the line or the StackPane, you can calculate the position of the StackPane based on the line properties like this



          stackPane.layoutX = (line.startX + line.endX - stackPane.width) / 2;


          (Procede accordingly for y coordinates.)



          transformX and transformY could simply be added, but general transforms would require you to




          1. Listen to changes of the transforms

          2. Use localToParent on the start/end coordinates of the line to get the location in the parent.


          I recommend using Bindings.createDoubleBindings for complicate double bindings btw, since this makes the formula for calculating the values much easier to read.



          Example



          I use a Label, since this provides background/border functionality too.



          @Override
          public void start(Stage primaryStage) throws IOException {
          Label label = new Label();
          label.setStyle("-fx-background-color: white; -fx-border-color: black;");
          label.setPadding(new Insets(2, 4, 2, 4));

          Line line = new Line(300, 300, 300, 100);

          label.layoutXProperty().bind(Bindings.createDoubleBinding(
          () -> (line.getStartX() + line.getEndX() - label.getWidth()) / 2,
          line.startXProperty(), line.endXProperty(), label.widthProperty()));
          label.layoutYProperty().bind(Bindings.createDoubleBinding(
          () -> (line.getStartY() + line.getEndY() - label.getHeight()) / 2,
          line.startYProperty(), line.endYProperty(), label.heightProperty()));

          DoubleProperty angle = new SimpleDoubleProperty();
          line.endXProperty().bind(Bindings.createDoubleBinding(() -> 300 + 200 * Math.sin(angle.get()), angle));
          line.endYProperty().bind(Bindings.createDoubleBinding(() -> 300 + 200 * Math.cos(angle.get()), angle));

          Timeline timeline = new Timeline(
          new KeyFrame(Duration.ZERO, new KeyValue(angle, 0d, Interpolator.LINEAR)),
          new KeyFrame(Duration.seconds(10), new KeyValue(angle, Math.PI * 2, Interpolator.LINEAR)));
          timeline.setCycleCount(Animation.INDEFINITE);
          label.textProperty().bind(timeline.currentTimeProperty().asString());
          timeline.play();

          Scene scene = new Scene(new Pane(line, label), 600, 600);

          primaryStage.setScene(scene);
          primaryStage.show();
          }





          share|improve this answer
























          • Thanks Fabian for the answer, this was really helpful.

            – Jaman
            Nov 15 '18 at 22:43














          1












          1








          1







          Assuming no transformations have been applied to the line or the StackPane, you can calculate the position of the StackPane based on the line properties like this



          stackPane.layoutX = (line.startX + line.endX - stackPane.width) / 2;


          (Procede accordingly for y coordinates.)



          transformX and transformY could simply be added, but general transforms would require you to




          1. Listen to changes of the transforms

          2. Use localToParent on the start/end coordinates of the line to get the location in the parent.


          I recommend using Bindings.createDoubleBindings for complicate double bindings btw, since this makes the formula for calculating the values much easier to read.



          Example



          I use a Label, since this provides background/border functionality too.



          @Override
          public void start(Stage primaryStage) throws IOException {
          Label label = new Label();
          label.setStyle("-fx-background-color: white; -fx-border-color: black;");
          label.setPadding(new Insets(2, 4, 2, 4));

          Line line = new Line(300, 300, 300, 100);

          label.layoutXProperty().bind(Bindings.createDoubleBinding(
          () -> (line.getStartX() + line.getEndX() - label.getWidth()) / 2,
          line.startXProperty(), line.endXProperty(), label.widthProperty()));
          label.layoutYProperty().bind(Bindings.createDoubleBinding(
          () -> (line.getStartY() + line.getEndY() - label.getHeight()) / 2,
          line.startYProperty(), line.endYProperty(), label.heightProperty()));

          DoubleProperty angle = new SimpleDoubleProperty();
          line.endXProperty().bind(Bindings.createDoubleBinding(() -> 300 + 200 * Math.sin(angle.get()), angle));
          line.endYProperty().bind(Bindings.createDoubleBinding(() -> 300 + 200 * Math.cos(angle.get()), angle));

          Timeline timeline = new Timeline(
          new KeyFrame(Duration.ZERO, new KeyValue(angle, 0d, Interpolator.LINEAR)),
          new KeyFrame(Duration.seconds(10), new KeyValue(angle, Math.PI * 2, Interpolator.LINEAR)));
          timeline.setCycleCount(Animation.INDEFINITE);
          label.textProperty().bind(timeline.currentTimeProperty().asString());
          timeline.play();

          Scene scene = new Scene(new Pane(line, label), 600, 600);

          primaryStage.setScene(scene);
          primaryStage.show();
          }





          share|improve this answer













          Assuming no transformations have been applied to the line or the StackPane, you can calculate the position of the StackPane based on the line properties like this



          stackPane.layoutX = (line.startX + line.endX - stackPane.width) / 2;


          (Procede accordingly for y coordinates.)



          transformX and transformY could simply be added, but general transforms would require you to




          1. Listen to changes of the transforms

          2. Use localToParent on the start/end coordinates of the line to get the location in the parent.


          I recommend using Bindings.createDoubleBindings for complicate double bindings btw, since this makes the formula for calculating the values much easier to read.



          Example



          I use a Label, since this provides background/border functionality too.



          @Override
          public void start(Stage primaryStage) throws IOException {
          Label label = new Label();
          label.setStyle("-fx-background-color: white; -fx-border-color: black;");
          label.setPadding(new Insets(2, 4, 2, 4));

          Line line = new Line(300, 300, 300, 100);

          label.layoutXProperty().bind(Bindings.createDoubleBinding(
          () -> (line.getStartX() + line.getEndX() - label.getWidth()) / 2,
          line.startXProperty(), line.endXProperty(), label.widthProperty()));
          label.layoutYProperty().bind(Bindings.createDoubleBinding(
          () -> (line.getStartY() + line.getEndY() - label.getHeight()) / 2,
          line.startYProperty(), line.endYProperty(), label.heightProperty()));

          DoubleProperty angle = new SimpleDoubleProperty();
          line.endXProperty().bind(Bindings.createDoubleBinding(() -> 300 + 200 * Math.sin(angle.get()), angle));
          line.endYProperty().bind(Bindings.createDoubleBinding(() -> 300 + 200 * Math.cos(angle.get()), angle));

          Timeline timeline = new Timeline(
          new KeyFrame(Duration.ZERO, new KeyValue(angle, 0d, Interpolator.LINEAR)),
          new KeyFrame(Duration.seconds(10), new KeyValue(angle, Math.PI * 2, Interpolator.LINEAR)));
          timeline.setCycleCount(Animation.INDEFINITE);
          label.textProperty().bind(timeline.currentTimeProperty().asString());
          timeline.play();

          Scene scene = new Scene(new Pane(line, label), 600, 600);

          primaryStage.setScene(scene);
          primaryStage.show();
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 21:15









          fabianfabian

          52.8k115373




          52.8k115373













          • Thanks Fabian for the answer, this was really helpful.

            – Jaman
            Nov 15 '18 at 22:43



















          • Thanks Fabian for the answer, this was really helpful.

            – Jaman
            Nov 15 '18 at 22:43

















          Thanks Fabian for the answer, this was really helpful.

          – Jaman
          Nov 15 '18 at 22:43





          Thanks Fabian for the answer, this was really helpful.

          – Jaman
          Nov 15 '18 at 22:43




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327646%2fadd-a-node-in-the-middle-of-a-line-java-fx%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python