studio programacion para móviles libro edición desarrollo desarrollar curso aprende aplicaciones java mouse

java - para - manual de programacion android pdf



Obtener la posición del mouse (10)

Me gustaría simular un movimiento natural del mouse en Java (yendo de aquí para allá píxel por píxel). Para hacer eso, necesito saber las coordenadas iniciales.

He encontrado el método event.getX () y event.getY () pero necesito un evento ...

¿Cómo puedo saber las posiciones sin hacer nada (o algo no visible)?

Gracias


En SWT no necesita estar en un oyente para obtener la ubicación del mouse. 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 aplicaciones SWT, es preferible utilizar getCursorLocation() sobre MouseInfo.getPointerInfo() que otros han mencionado, ya que este último se implementa en el kit de herramientas AWT que SWT fue diseñado para reemplazar.


En mi caso, se suponía que debía abrir un cuadro de diálogo en la posición del mouse en función de una operación de GUI realizada con el mouse. 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; }


Estoy haciendo algo como esto para obtener coordenadas del mouse usando Robot, uso estas coordenadas aún 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; } } } }


Intenta mirar la clase java.awt.Robot. Le permite mover el mouse programáticamente.


Si está utilizando SWT, le recomendamos que agregue un MouseMoveListener como se explica here .




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


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 + ")"); } } }


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 } } }