D
D
DR_Demons2014-06-01 19:10:10
Java
DR_Demons, 2014-06-01 19:10:10

How to get the center of the screen and put the mouse cursor there in java?

Hello! I study java, I train with swing, awt. A simple program, by pressing a button, the cursor should move to the center of the screen. I calculate the center of the screen like this

Dimension sSize = Toolkit.getDefaultToolkit ().getScreenSize ();
      int h = sSize.height;
      int w = sSize.width;

I divide the received values ​​​​by two and transfer them
Robot robot = new Robot();
      robot.mouseMove(x, y);

But the cursor does not move to the center, but to the bottom edge of the screen. Tell me, what could be the error, or maybe there is some ready-made method for calculating the center?
Part of the code on the topic
class ButtonListener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
      Dimension sSize = Toolkit.getDefaultToolkit ().getScreenSize ();
      int h = sSize.height/2;
      int w = sSize.width/2;
      System.out.println("H = " + h + " W = " + w);
      mouseMoveGo(h, w);
      frame.remove(button);
      frame.add(label);
      frame.validate();
      frame.repaint();
      
      
    }
  }
  
  public void mouseMoveGo(int x, int y) {
    try {
      Robot robot = new Robot();
      robot.mouseMove(x, y);
      System.out.println("X = " + x + " Y = " + y);
    } catch (Exception ex) {ex.printStackTrace();}
    
  }

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dm13y, 2014-06-01
@DR_Demons

You have the following problem:
You call the method with the following parameters mouseMoveGo(Height, Width).
And in the method itself, you use the parameters in reverse order (Width, Height).

Similar questions
K
KhanTengri2011-06-17 23:56:41
Get from a remote web page?</a> <a class="content__item-button" href="/q/get-title-from-a-remote-web-page"> <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" class=""> <path fill-rule="evenodd" d="M15 14a1.002 1.002 0 01-1.703.71h-.004l-.003-.01-1.704-1.7H3c-1.104 0-2-.9-2-2V3c0-1.1.896-2 2-2h10c1.104 0 2 .9 2 2v11zM5 6c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1zm3 0c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1zm3 0c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1z"></path> </svg> <span class="dark">6</span><span class="dn">Reply</span> </a> </div> </div><div class="content__item"> <div class="content__item-img name-color-S"> <span>S</span></div> <div class="content__item-content"> <div class="content__item-row"> <a href="/q/does-it-make-sense-to-use-spring-boot">Sergey Nizhny Novgorod</a><span>2018-02-17 16:43:40</span></div> <a class="content__item-title similar" href="/q/does-it-make-sense-to-use-spring-boot">Does it make sense to use Spring Boot?</a> <a class="content__item-button" href="/q/does-it-make-sense-to-use-spring-boot"> <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16" class=""> <path fill-rule="evenodd" d="M15 14a1.002 1.002 0 01-1.703.71h-.004l-.003-.01-1.704-1.7H3c-1.104 0-2-.9-2-2V3c0-1.1.896-2 2-2h10c1.104 0 2 .9 2 2v11zM5 6c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1zm3 0c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1zm3 0c.552 0 1 .45 1 1s-.448 1-1 1-1-.45-1-1 .448-1 1-1z"></path> </svg> <span class="dark">1</span><span class="dn">Reply</span> </a> </div> </div> </div> </div> </div> <div class="content find"> <p class="find__p">Didn't find what you were looking for?</p><a class="find__btn" href="#!">Ask your question</a> </div> <div class="find sm"> <p class="find__title">Ask a Question </p> <form class="find__form" action="#!"> <input class="find__input" type="text" placeholder="Ask your question here"> <button class="find__btn" type="submit"> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 16 16" class="nBqR1"><path fill-rule="evenodd" d="M12.696 7.256L9.657 4.217a1 1 0 00-1.415 1.414l1.344 1.344H3.949a1.001 1.001 0 000 2h5.637l-1.344 1.343a1.002 1.002 0 000 1.415c.391.39 1.024.39 1.415 0l3.039-3.04A.993.993 0 0013 7.975a.997.997 0 00-.293-.708l-.011-.011z"></path></svg></button> </form> <p class="find__span">731 491 924 answers to any question</p> </div> </div> <aside class="banners"> <p>banners </p> </aside> </div> </main> </div> <script src="/assets/theme/js/highlight/highlight.min.js"></script> <script src="/assets/theme/js/app.min.js?_v=20220404145216"> </script> <script>hljs.highlightAll();</script> </body> </html>