如果找到了对您有用的资料,烦请点击右手边的Google广告支持我继续共享知识,谢谢! http://dengpeng.spaces.live.com/

2007年7月10日星期二

Java获得当前鼠标指针在屏幕上的绝对坐标,屏幕尺寸和分辨率并自动移动鼠标到指定位置

/*
 * Main.java
 *
 * Created on 2007年7月10日, 下午2:01
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
 
import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;
import java.awt.Toolkit;
 
/**
 *
 * @author Peng
 */
public class Main {
    private static Dimension dim;
    //private Robot robot;
    
    /** Creates a new instance of Main */
    public Main() {
        //robot = new Robot();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        
        int scrRes=Toolkit.getDefaultToolkit().getScreenResolution();
        System.out.println("The resolution of screen is "+ scrRes);
        dim=Toolkit.getDefaultToolkit().getScreenSize();
        System.out.println("Screen Width is "+dim.getWidth());
        System.out.println("Screen height is "+ dim.getHeight());
        Toolkit.getDefaultToolkit().beep(); //Host OS beep, not computer BIOS beep
        
        double targetX=dim.getWidth();
        double targetY=dim.getHeight();
        
        PointerInfo pt;
        Point Cursorlocation;
        double x,y;
        
        boolean flag=true;
        while(flag){
            pt=MouseInfo.getPointerInfo();
            Cursorlocation=pt.getLocation();
            x=Cursorlocation.getX();
            y=Cursorlocation.getY();
            System.out.println("Current mouse cursor position X: "+x);
            System.out.println("Current mouse cursor position Y: "+y);
            
            if(x==targetX-1 && y==targetY-1)
                flag=false;
            if(x<targetX)
                x++;
            if(x>targetX)
                x--;
            if(y<targetY)
                y++;
            if(y>targetY)
                y--;
            move(x,y);
            //robot.mouseMove((int)x,(int)y);
            try {
                Thread.sleep(40);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
        System.out.println("Application terminate");
    }
 
    private static void move(double x, double y) {
        Robot r;
        try {
            r = new Robot();
            r.mouseMove((int)x,(int)y);
        } catch (AWTException ex) {
            ex.printStackTrace();
        }
        
    }
    
}

Java代码获得当前鼠标指针在屏幕上的绝对坐标,获得当前屏幕的尺寸,分辨率,让系统发出提示音。


 

没有评论: