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).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question