2017-10-06 6 views
0

커서를 화면의 픽셀 사각형에 보내는 방법을 찾으려고합니다. 자, 내가 특정 위치로 보낼 수 일부 코드를 가지고 :커서를 픽셀의 제곱에 보냄

package JavaObjects; 
import java.awt.AWTException; 
import java.awt.Robot; 

public class MCur { 
    public static void main(String args[]) { 
     try { 
      // The cursor goes to these coordinates 
      int xCoord = 500; 
      int yCoord = 500; 

      // This moves the cursor 
      Robot robot = new Robot(); 
      robot.mouseMove(xCoord, yCoord); 
     } catch (AWTException e) {} 
    } 
} 

아마도 어떤 식 으로든 그와 유사한 코드를 사용하여, 거기에, 나는 특정 지점이 아닌 범위를 설정할 수 있습니다와 같은 커서는 확립 된 광장의 임의의 부분으로 간다?

+0

'''java.util.Random'''이 있는지 알고 싶습니까? 네, 그렇습니다. ''xmin'''과''xmax''' (포함) 사이에''x'' 좌표를 생성합니다 :'''x = xmin + r.nextInt (xmax-xmin + 1)''' – tevemadar

답변

2

"Square"라고 말하면서 작업하고 있기 때문에 java.awt.Rectangle 클래스를 사용하고 싶을 수도 있습니다. 버튼을 클릭하면 버튼 경계를 정의 할 수 있으므로 특히 유용합니다. 요점. 임의의 반경으로

이 쉽게 java.util.Random의

import java.awt.AWTException; 
import java.awt.Dimension; 
import java.awt.Rectangle; 
import java.awt.Robot; 
import java.awt.Toolkit; 
import java.util.Random; 

public class MoveMouse { 

    private static final Robot ROBOT; 
    private static final Random RNG; 

    public static void main(String[] args) { 
     // grab the screen size 
     Dimension screen = Toolkit.getDefaultToolkit().getScreenSize(); 
     // Equivalent to 'new Rectangle(0, 0, screen.width, screen.height)' 
     Rectangle boundary = new Rectangle(screen); 
     // move anywhere on screen 
     moveMouse(boundary); 
    } 

    public static void moveMouse(int x, int y, int radiusX, int radiusY) { 
     Rectangle boundary = new Rectangle(); 
     // this will be our center 
     boundary.setLocation(x, y); 
     // grow the boundary from the center 
     boundary.grow(radiusX, radiusY); 
     moveMouse(boundary); 
    } 

    public static void moveMouse(Rectangle boundary) { 
     // add 1 to the width/height, nextInt returns an exclusive random number (0 to (argument - 1)) 
     int x = boundary.x + RNG.nextInt(boundary.width + 1); 
     int y = boundary.y + RNG.nextInt(boundary.height + 1); 
     ROBOT.mouseMove(x, y); 
    } 

    // initialize the robot/random instance once when the class is loaded 
    // and throw an exception in the unlikely scenario when it can't 
    static { 
     try { 
      ROBOT = new Robot(); 
      RNG = new Random(); 
     } catch (Exception e) { 
      throw new RuntimeException(e); 
     } 
    } 

} 

로 이루어집니다 이것은 기본 설명입니다.

음수/범위를 벗어나는 값 확인 등을 추가하여 화면을 클릭하지 않도록 할 수 있습니다.

+0

도움을 주셔서 감사합니다! 나는 그것을 컴파일 할려고하지만, 실행하려고 할 때 String []을 받아들이는 정적 void main 메서드를 가진 클래스가 없다고 말한다. 위에 포함 된 코드에서 "public static void main (String args []) {"이 동일한 문제를 해결하기 위해 줄을 사용했지만 여기서이 줄을 사용하면 "insert enum 식별자 "를"static { "행에 입력하십시오. 내가 뭘 할 수 있는지 알아? 나는 당신의 머리 꼭대기에서 잘 알지 못한다면 이런 종류의 것들을 진단하는 것이 어렵다는 것을 이해합니다. – RealFL

+0

클래스 가져 오기/클래스 body/main 메소드를 추가했습니다. 구문 및 기본 언어 기능을 실제로 시도하기 전에. – Caleb