2009-09-17 35 views
86

Mi piacerebbe simulare un movimento naturale del mouse in Java (passando da qui a lì pixel per pixel). Per farlo ho bisogno di conoscere le coordinate iniziali.Ottiene la posizione del mouse

ho trovato il metodo event.getX() e event.getY(), ma ho bisogno di un evento ...

Come posso sapere le posizioni senza fare nulla (o qualcosa di non visibile)?

Grazie

risposta

178

MouseInfo.getPointerInfo().getLocation() potrebbe essere utile. Restituisce un oggetto Point corrispondente alla posizione corrente del mouse.

+40

'getPointerInfo(). GetLocation()' restituisce la posizione relativa allo schermo. Se vuoi la posizione relativa al tuo componente (come indicato da MouseListeners) puoi sottrarre 'yourComponent.getLocationOnScreen()' da esso. –

+2

+1 'Container.getMousePosition()' può restituire alcune volte 'null' se il mouse si muove troppo velocemente, usando questo si evita il problema. –

+0

possiamo sapere dove si fa clic sul mouse sullo schermo naturale – Srb

2

Se stai usando swing come livello di interfaccia utente, è possibile utilizzare un Mouse-Motion Listener per questo.

2

Prova a guardare la classe java.awt.Robot. Ti permette di muovere il mouse programmaticamente.

0

Se si utilizza SWT, si potrebbe voler aggiungere un MouseMoveListener come spiegato here.

+4

ma il Listener viene eseguito solo se faccio qualcosa (muoviti, clic) con il mio mouse, giusto?La prima cosa di cui ho bisogno prima di spostarlo è conoscere la posizione di partenza –

30
PointerInfo a = MouseInfo.getPointerInfo(); 
Point b = a.getLocation(); 
int x = (int) b.getX(); 
int y = (int) b.getY(); 
System.out.print(y + "jjjjjjjjj"); 
System.out.print(x); 
Robot r = new Robot(); 
r.mouseMove(x, y - 50); 
+13

Per favore aggiungi la prossima volta alcuni commenti. – CSchulz

8

In SWT non è necessario essere in un listener per ottenere la posizione del mouse. L'oggetto Display ha il metodo getCursorLocation().

In vaniglia SWT/JFace, chiamare Display.getCurrent().getCursorLocation().

In un'applicazione RCP, chiamare PlatformUI.getWorkbench().getDisplay().getCursorLocation().

Per le applicazioni SWT, è preferibile utilizzare getCursorLocation() su MouseInfo.getPointerInfo() che altri hanno menzionato, poiché quest'ultimo è implementato nel toolkit AWT che SWT è stato progettato per sostituire.

0

Nel mio scenario, dovevo aprire una finestra di dialogo nella posizione del mouse basata su un'operazione della GUI eseguita con il mouse. Il seguente codice ha funzionato per me:

public Object open() { 
    //create the contents of the dialog 
    createContents(); 
    //setting the shell location based on the curent position 
    //of the mouse 
    PointerInfo a = MouseInfo.getPointerInfo(); 
    Point pt = a.getLocation(); 
    shellEO.setLocation (pt.x, pt.y); 

    //once the contents are created and location is set- 
    //open the dialog 
    shellEO.open(); 
    shellEO.layout(); 
    Display display = getParent().getDisplay(); 
    while (!shellEO.isDisposed()) { 
     if (!display.readAndDispatch()) { 
      display.sleep(); 
     } 
    } 
    return result; 
} 
5
import java.awt.MouseInfo; 
import java.awt.GridLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseEvent; 

import javax.swing.*; 

public class MyClass { 
    public static void main(String[] args) throws InterruptedException{ 
    while(true){ 
     //Thread.sleep(100); 
     System.out.println("(" + MouseInfo.getPointerInfo().getLocation().x + 
       ", " + 
       MouseInfo.getPointerInfo().getLocation().y + ")"); 
    } 
    } 
} 
4
import java.awt.MouseInfo; 
import java.util.concurrent.TimeUnit; 

public class Cords { 

    public static void main(String[] args) throws InterruptedException { 

     //get cords of mouse code, outputs to console every 1/2 second 
     //make sure to import and include the "throws in the main method" 

     while(true == true) 
     { 
     TimeUnit.SECONDS.sleep(1/2); 
     double mouseX = MouseInfo.getPointerInfo().getLocation().getX(); 
     double mouseY = MouseInfo.getPointerInfo().getLocation().getY(); 
     System.out.println("X:" + mouseX); 
     System.out.println("Y:" + mouseY); 
     //make sure to import 
     } 

    } 

} 
0

sto facendo qualcosa di simile a questo per ottenere il mouse le coordinate utilizzando robot, io uso queste coordinate ulteriormente alcuni dei giochi che sto sviluppando:

public class ForMouseOnly { 
    public static void main(String[] args) throws InterruptedException { 
     int x = MouseInfo.getPointerInfo().getLocation().x; 
     int y = MouseInfo.getPointerInfo().getLocation().y; 
     while (true) { 

      if (x != MouseInfo.getPointerInfo().getLocation().x || y != MouseInfo.getPointerInfo().getLocation().y) { 
       System.out.println("(" + MouseInfo.getPointerInfo().getLocation().x + ", " 
         + MouseInfo.getPointerInfo().getLocation().y + ")"); 
       x = MouseInfo.getPointerInfo().getLocation().x; 
       y = MouseInfo.getPointerInfo().getLocation().y; 
      } 
     } 
    } 
} 
Problemi correlati