How can I get the center coordinates of a rectangle in javafx?












-1















do anyone has an idea on how to get the center-coordinates of an rectangle in javafx?
Is there a method or an algorithm for that, couse I want to know the center-position while the rectangle is rotating and moving.
I couldn't find a solution on the web so I hope one of you can help me.










share|improve this question



























    -1















    do anyone has an idea on how to get the center-coordinates of an rectangle in javafx?
    Is there a method or an algorithm for that, couse I want to know the center-position while the rectangle is rotating and moving.
    I couldn't find a solution on the web so I hope one of you can help me.










    share|improve this question

























      -1












      -1








      -1








      do anyone has an idea on how to get the center-coordinates of an rectangle in javafx?
      Is there a method or an algorithm for that, couse I want to know the center-position while the rectangle is rotating and moving.
      I couldn't find a solution on the web so I hope one of you can help me.










      share|improve this question














      do anyone has an idea on how to get the center-coordinates of an rectangle in javafx?
      Is there a method or an algorithm for that, couse I want to know the center-position while the rectangle is rotating and moving.
      I couldn't find a solution on the web so I hope one of you can help me.







      javafx






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 10:27









      lukasQlukasQ

      11




      11
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You can calculate the center of a rectangle like this:



          center.x = position.x + width / 2
          center.y = position.y + height / 2


          Or if you represent your rectangle as 2 points (two opposite corners) you would have to use the following formula:



          p0
          +-------+
          | |
          | |
          +-------+
          p1

          center.x = (p0.x + p1.x) / 2
          center.y = (p0.y + p1.y) / 2


          Also:
          A quick google yields How to find the Center Coordinate of Rectangle? as first result...






          share|improve this answer


























          • thanks for your answer but I think this method is useless while the rectangle is rotating, isn't it?

            – lukasQ
            Jan 1 at 11:00











          • It works perfectly fine for a rotated rectangle. Imagine drawing a diagonal line through your rectangle and then marking the center of this line. It will also work on a scaled, translated and sheared rectangle.

            – Kanjiu
            Jan 1 at 11:08













          • Wait... do you know the rotated points? Or is that your problem?

            – Kanjiu
            Jan 1 at 11:14











          • Sorry I am a beginner. How do one keep track of these opposite points? As far as I know the methods .getTranslatex() / .getTranslateY() always takes the most left and the uppest corner of the rectangle.

            – lukasQ
            Jan 1 at 11:21






          • 1





            Note you probably want to compute the center based on the rectangle's boundsInParent property; then all transforms are taken into account.

            – Slaw
            Jan 1 at 12:26





















          1














          Use the localToParent method to convert the center of the rectangle to the coordinate system of the parent.



          The center of the Rectangle in local coordinates is



          x = rect.x + rect.width/2
          y = rect.y + rect.height/2


          Example



          @Override
          public void start(Stage primaryStage) throws Exception {
          Rectangle rect = new Rectangle(50, 50, 100, 100);
          Translate translate = new Translate();
          Rotate rotate = new Rotate(0, 0, 0);
          rect.getTransforms().addAll(translate, rotate);
          Circle circle = new Circle(5, Color.RED);

          AnimationTimer timer = new AnimationTimer() {

          @Override
          public void handle(long now) {
          double d = now / 5_000_000_000d;
          rotate.setAngle(d * 360);
          translate.setX((d % 2) * 300);
          translate.setY((d % 3) * 150);

          // set circle center to coordinates of rect's center
          Point2D center = rect.localToParent(rect.getX() + 0.5 * rect.getWidth(),
          rect.getY() + 0.5 * rect.getHeight());
          circle.setCenterX(center.getX());
          circle.setCenterY(center.getY());
          }

          };
          Pane root = new Pane(rect, circle);

          timer.start();

          Scene scene = new Scene(root, 800, 800);
          primaryStage.setScene(scene);
          primaryStage.show();
          }





          share|improve this answer























            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%2f53994718%2fhow-can-i-get-the-center-coordinates-of-a-rectangle-in-javafx%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You can calculate the center of a rectangle like this:



            center.x = position.x + width / 2
            center.y = position.y + height / 2


            Or if you represent your rectangle as 2 points (two opposite corners) you would have to use the following formula:



            p0
            +-------+
            | |
            | |
            +-------+
            p1

            center.x = (p0.x + p1.x) / 2
            center.y = (p0.y + p1.y) / 2


            Also:
            A quick google yields How to find the Center Coordinate of Rectangle? as first result...






            share|improve this answer


























            • thanks for your answer but I think this method is useless while the rectangle is rotating, isn't it?

              – lukasQ
              Jan 1 at 11:00











            • It works perfectly fine for a rotated rectangle. Imagine drawing a diagonal line through your rectangle and then marking the center of this line. It will also work on a scaled, translated and sheared rectangle.

              – Kanjiu
              Jan 1 at 11:08













            • Wait... do you know the rotated points? Or is that your problem?

              – Kanjiu
              Jan 1 at 11:14











            • Sorry I am a beginner. How do one keep track of these opposite points? As far as I know the methods .getTranslatex() / .getTranslateY() always takes the most left and the uppest corner of the rectangle.

              – lukasQ
              Jan 1 at 11:21






            • 1





              Note you probably want to compute the center based on the rectangle's boundsInParent property; then all transforms are taken into account.

              – Slaw
              Jan 1 at 12:26


















            1














            You can calculate the center of a rectangle like this:



            center.x = position.x + width / 2
            center.y = position.y + height / 2


            Or if you represent your rectangle as 2 points (two opposite corners) you would have to use the following formula:



            p0
            +-------+
            | |
            | |
            +-------+
            p1

            center.x = (p0.x + p1.x) / 2
            center.y = (p0.y + p1.y) / 2


            Also:
            A quick google yields How to find the Center Coordinate of Rectangle? as first result...






            share|improve this answer


























            • thanks for your answer but I think this method is useless while the rectangle is rotating, isn't it?

              – lukasQ
              Jan 1 at 11:00











            • It works perfectly fine for a rotated rectangle. Imagine drawing a diagonal line through your rectangle and then marking the center of this line. It will also work on a scaled, translated and sheared rectangle.

              – Kanjiu
              Jan 1 at 11:08













            • Wait... do you know the rotated points? Or is that your problem?

              – Kanjiu
              Jan 1 at 11:14











            • Sorry I am a beginner. How do one keep track of these opposite points? As far as I know the methods .getTranslatex() / .getTranslateY() always takes the most left and the uppest corner of the rectangle.

              – lukasQ
              Jan 1 at 11:21






            • 1





              Note you probably want to compute the center based on the rectangle's boundsInParent property; then all transforms are taken into account.

              – Slaw
              Jan 1 at 12:26
















            1












            1








            1







            You can calculate the center of a rectangle like this:



            center.x = position.x + width / 2
            center.y = position.y + height / 2


            Or if you represent your rectangle as 2 points (two opposite corners) you would have to use the following formula:



            p0
            +-------+
            | |
            | |
            +-------+
            p1

            center.x = (p0.x + p1.x) / 2
            center.y = (p0.y + p1.y) / 2


            Also:
            A quick google yields How to find the Center Coordinate of Rectangle? as first result...






            share|improve this answer















            You can calculate the center of a rectangle like this:



            center.x = position.x + width / 2
            center.y = position.y + height / 2


            Or if you represent your rectangle as 2 points (two opposite corners) you would have to use the following formula:



            p0
            +-------+
            | |
            | |
            +-------+
            p1

            center.x = (p0.x + p1.x) / 2
            center.y = (p0.y + p1.y) / 2


            Also:
            A quick google yields How to find the Center Coordinate of Rectangle? as first result...







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 1 at 11:06

























            answered Jan 1 at 10:48









            KanjiuKanjiu

            42110




            42110













            • thanks for your answer but I think this method is useless while the rectangle is rotating, isn't it?

              – lukasQ
              Jan 1 at 11:00











            • It works perfectly fine for a rotated rectangle. Imagine drawing a diagonal line through your rectangle and then marking the center of this line. It will also work on a scaled, translated and sheared rectangle.

              – Kanjiu
              Jan 1 at 11:08













            • Wait... do you know the rotated points? Or is that your problem?

              – Kanjiu
              Jan 1 at 11:14











            • Sorry I am a beginner. How do one keep track of these opposite points? As far as I know the methods .getTranslatex() / .getTranslateY() always takes the most left and the uppest corner of the rectangle.

              – lukasQ
              Jan 1 at 11:21






            • 1





              Note you probably want to compute the center based on the rectangle's boundsInParent property; then all transforms are taken into account.

              – Slaw
              Jan 1 at 12:26





















            • thanks for your answer but I think this method is useless while the rectangle is rotating, isn't it?

              – lukasQ
              Jan 1 at 11:00











            • It works perfectly fine for a rotated rectangle. Imagine drawing a diagonal line through your rectangle and then marking the center of this line. It will also work on a scaled, translated and sheared rectangle.

              – Kanjiu
              Jan 1 at 11:08













            • Wait... do you know the rotated points? Or is that your problem?

              – Kanjiu
              Jan 1 at 11:14











            • Sorry I am a beginner. How do one keep track of these opposite points? As far as I know the methods .getTranslatex() / .getTranslateY() always takes the most left and the uppest corner of the rectangle.

              – lukasQ
              Jan 1 at 11:21






            • 1





              Note you probably want to compute the center based on the rectangle's boundsInParent property; then all transforms are taken into account.

              – Slaw
              Jan 1 at 12:26



















            thanks for your answer but I think this method is useless while the rectangle is rotating, isn't it?

            – lukasQ
            Jan 1 at 11:00





            thanks for your answer but I think this method is useless while the rectangle is rotating, isn't it?

            – lukasQ
            Jan 1 at 11:00













            It works perfectly fine for a rotated rectangle. Imagine drawing a diagonal line through your rectangle and then marking the center of this line. It will also work on a scaled, translated and sheared rectangle.

            – Kanjiu
            Jan 1 at 11:08







            It works perfectly fine for a rotated rectangle. Imagine drawing a diagonal line through your rectangle and then marking the center of this line. It will also work on a scaled, translated and sheared rectangle.

            – Kanjiu
            Jan 1 at 11:08















            Wait... do you know the rotated points? Or is that your problem?

            – Kanjiu
            Jan 1 at 11:14





            Wait... do you know the rotated points? Or is that your problem?

            – Kanjiu
            Jan 1 at 11:14













            Sorry I am a beginner. How do one keep track of these opposite points? As far as I know the methods .getTranslatex() / .getTranslateY() always takes the most left and the uppest corner of the rectangle.

            – lukasQ
            Jan 1 at 11:21





            Sorry I am a beginner. How do one keep track of these opposite points? As far as I know the methods .getTranslatex() / .getTranslateY() always takes the most left and the uppest corner of the rectangle.

            – lukasQ
            Jan 1 at 11:21




            1




            1





            Note you probably want to compute the center based on the rectangle's boundsInParent property; then all transforms are taken into account.

            – Slaw
            Jan 1 at 12:26







            Note you probably want to compute the center based on the rectangle's boundsInParent property; then all transforms are taken into account.

            – Slaw
            Jan 1 at 12:26















            1














            Use the localToParent method to convert the center of the rectangle to the coordinate system of the parent.



            The center of the Rectangle in local coordinates is



            x = rect.x + rect.width/2
            y = rect.y + rect.height/2


            Example



            @Override
            public void start(Stage primaryStage) throws Exception {
            Rectangle rect = new Rectangle(50, 50, 100, 100);
            Translate translate = new Translate();
            Rotate rotate = new Rotate(0, 0, 0);
            rect.getTransforms().addAll(translate, rotate);
            Circle circle = new Circle(5, Color.RED);

            AnimationTimer timer = new AnimationTimer() {

            @Override
            public void handle(long now) {
            double d = now / 5_000_000_000d;
            rotate.setAngle(d * 360);
            translate.setX((d % 2) * 300);
            translate.setY((d % 3) * 150);

            // set circle center to coordinates of rect's center
            Point2D center = rect.localToParent(rect.getX() + 0.5 * rect.getWidth(),
            rect.getY() + 0.5 * rect.getHeight());
            circle.setCenterX(center.getX());
            circle.setCenterY(center.getY());
            }

            };
            Pane root = new Pane(rect, circle);

            timer.start();

            Scene scene = new Scene(root, 800, 800);
            primaryStage.setScene(scene);
            primaryStage.show();
            }





            share|improve this answer




























              1














              Use the localToParent method to convert the center of the rectangle to the coordinate system of the parent.



              The center of the Rectangle in local coordinates is



              x = rect.x + rect.width/2
              y = rect.y + rect.height/2


              Example



              @Override
              public void start(Stage primaryStage) throws Exception {
              Rectangle rect = new Rectangle(50, 50, 100, 100);
              Translate translate = new Translate();
              Rotate rotate = new Rotate(0, 0, 0);
              rect.getTransforms().addAll(translate, rotate);
              Circle circle = new Circle(5, Color.RED);

              AnimationTimer timer = new AnimationTimer() {

              @Override
              public void handle(long now) {
              double d = now / 5_000_000_000d;
              rotate.setAngle(d * 360);
              translate.setX((d % 2) * 300);
              translate.setY((d % 3) * 150);

              // set circle center to coordinates of rect's center
              Point2D center = rect.localToParent(rect.getX() + 0.5 * rect.getWidth(),
              rect.getY() + 0.5 * rect.getHeight());
              circle.setCenterX(center.getX());
              circle.setCenterY(center.getY());
              }

              };
              Pane root = new Pane(rect, circle);

              timer.start();

              Scene scene = new Scene(root, 800, 800);
              primaryStage.setScene(scene);
              primaryStage.show();
              }





              share|improve this answer


























                1












                1








                1







                Use the localToParent method to convert the center of the rectangle to the coordinate system of the parent.



                The center of the Rectangle in local coordinates is



                x = rect.x + rect.width/2
                y = rect.y + rect.height/2


                Example



                @Override
                public void start(Stage primaryStage) throws Exception {
                Rectangle rect = new Rectangle(50, 50, 100, 100);
                Translate translate = new Translate();
                Rotate rotate = new Rotate(0, 0, 0);
                rect.getTransforms().addAll(translate, rotate);
                Circle circle = new Circle(5, Color.RED);

                AnimationTimer timer = new AnimationTimer() {

                @Override
                public void handle(long now) {
                double d = now / 5_000_000_000d;
                rotate.setAngle(d * 360);
                translate.setX((d % 2) * 300);
                translate.setY((d % 3) * 150);

                // set circle center to coordinates of rect's center
                Point2D center = rect.localToParent(rect.getX() + 0.5 * rect.getWidth(),
                rect.getY() + 0.5 * rect.getHeight());
                circle.setCenterX(center.getX());
                circle.setCenterY(center.getY());
                }

                };
                Pane root = new Pane(rect, circle);

                timer.start();

                Scene scene = new Scene(root, 800, 800);
                primaryStage.setScene(scene);
                primaryStage.show();
                }





                share|improve this answer













                Use the localToParent method to convert the center of the rectangle to the coordinate system of the parent.



                The center of the Rectangle in local coordinates is



                x = rect.x + rect.width/2
                y = rect.y + rect.height/2


                Example



                @Override
                public void start(Stage primaryStage) throws Exception {
                Rectangle rect = new Rectangle(50, 50, 100, 100);
                Translate translate = new Translate();
                Rotate rotate = new Rotate(0, 0, 0);
                rect.getTransforms().addAll(translate, rotate);
                Circle circle = new Circle(5, Color.RED);

                AnimationTimer timer = new AnimationTimer() {

                @Override
                public void handle(long now) {
                double d = now / 5_000_000_000d;
                rotate.setAngle(d * 360);
                translate.setX((d % 2) * 300);
                translate.setY((d % 3) * 150);

                // set circle center to coordinates of rect's center
                Point2D center = rect.localToParent(rect.getX() + 0.5 * rect.getWidth(),
                rect.getY() + 0.5 * rect.getHeight());
                circle.setCenterX(center.getX());
                circle.setCenterY(center.getY());
                }

                };
                Pane root = new Pane(rect, circle);

                timer.start();

                Scene scene = new Scene(root, 800, 800);
                primaryStage.setScene(scene);
                primaryStage.show();
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 1 at 12:33









                fabianfabian

                52.6k115373




                52.6k115373






























                    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%2f53994718%2fhow-can-i-get-the-center-coordinates-of-a-rectangle-in-javafx%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

                    MongoDB - Not Authorized To Execute Command

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                    How to fix TextFormField cause rebuild widget in Flutter