Obtener la Posición del Ratón
Me gustaría simular un movimiento natural del ratón en Java (ir de aquí para allá píxel por píxel). Para hacer eso necesito saber las coordenadas de partida.
He encontrado el evento del método.getX () y event.getY() pero necesito un evento...
¿Cómo puedo saber las posiciones sin hacer nada (o algo no visible)?
Gracias
10 answers
MouseInfo.getPointerInfo().getLocation () podría ser útil. Devuelve un objeto Point correspondiente a la posición actual del ratón.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-11-01 23:23:56
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);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-06 22:52:02
En SWT no es necesario estar en un oyente para llegar a la ubicación del ratón. El objeto Display tiene el método getCursorLocation()
.
En vanilla SWT/JFace, llame a Display.getCurrent().getCursorLocation()
.
En una aplicación RCP, llame a PlatformUI.getWorkbench().getDisplay().getCursorLocation()
.
Para las aplicaciones SWT, es preferible usar getCursorLocation()
sobre el MouseInfo.getPointerInfo()
que otros han mencionado, ya que este último está implementado en el kit de herramientas AWT que SWT fue diseñado para reemplazar.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-02-02 23:14:44
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 + ")");
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-01 03:27:53
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
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-09-12 14:49:59
Intenta mirar el java.awt.Clase robot. Le permite mover el ratón programáticamente.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-09-17 13:59:44
Si usas Swing como capa de interfaz de usuario, puedes usar un Oyente de movimiento del ratón para esto.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-09-17 13:58:21
Si está utilizando SWT, es posible que desee agregar un MouseMoveListener como se explica aquí.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-09-17 14:01:19
En mi escenario, se suponía que debía abrir un cuadro de diálogo en la posición del ratón basado en una operación GUI realizada con el ratón. El siguiente código funcionó para mí:
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;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-17 14:45:58
Estoy haciendo algo como esto para obtener coordenadas de ratón usando Robot, utilizo estas coordenadas más en algunos de los juegos que estoy desarrollando:
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;
}
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-18 10:17:10