java问题请教
发布网友
发布时间:2023-08-21 19:02
我来回答
共1个回答
热心网友
时间:2024-12-05 10:13
//: RobotTest.java
// 要求 JDK 1.5 以上
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class RobotTest {
public static void main(String[] args) throws Exception {
final Robot rob = new Robot();
int x,y,w,h,delay;
delay=getNumber(1,10,"请输入鼠标停留时间,单位秒");
x = getNumber(0,500,"请输入X点数值");
y = getNumber(0,200,"请输入Y点数值");
w = getNumber(100,500,"请输入敏感区宽度");
h = getNumber(100,300,"请输入敏感区高度");
JOptionPane.showMessageDialog(null,"如果你的鼠标在 x="+x+",y="+y+",w="+w+",h="+h+" 的区域内\n停留超过"+delay+"秒钟以上,程式将退出.");
final Rectangle rect = new Rectangle(x,y,w,h);
BufferedImage bimg = rob.createScreenCapture(rect);
Graphics g = bimg.getGraphics();
g.setColor(new Color(0x55ffff00,true));
g.fillRect(0,0,w,h);
g.dispose();
//加入可见组件,使程式更直观
final JLabel l = new JLabel();
l.setIcon(new ImageIcon(bimg));
l.setHorizontalTextPosition(JLabel.CENTER);
l.setVerticalTextPosition(JLabel.CENTER);
l.setBorder(BorderFactory.createLineBorder(Color.blue));
final JWindow win = new JWindow();
win.add(l);
win.setAlwaysOnTop(true);
win.setBounds(rect);
win.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
win.setVisible(true);
final int d = delay;
//启动线程监视鼠标位置,如果 鼠标 在指定的区域停 d 秒钟,程序就退出
new Thread(new Runnable(){
public void run() {
long x = -1;
while(true){
Point p = MouseInfo.getPointerInfo().getLocation();
if(rect.contains(p)){
x = x==-1?System.currentTimeMillis():x;
if(x+d*1000<System.currentTimeMillis()){
win.dispose();
//移动鼠标到 (0,0)位置好了;
rob.mouseMove(0,0);
JOptionPane.showMessageDialog(null,"鼠标已移动到(0,0)位置,点击本对话框后程式将结束!");
break;
}
int re = d-(int)((System.currentTimeMillis()-x)/1000);
l.setText("<html><font size=8 color=blue>倒计时:<b>"+re);
}
else{
x=-1;
l.setText("<html><font color=blue size=6>请把鼠标移上来.");
}
try{Thread.sleep(20);}catch(Exception e){}
}
}
}).start();
}
private static int getNumber(int min,int max,String inf){
int i=min-1;
do{
String s = JOptionPane.showInputDialog(inf+"\n范围:"+min+"~"+max);
try{
i = Integer.parseInt(s.trim());
}catch(Exception e){}
}
while(i<min||i>max);
return i;
}
}