【发布时间】:2013-12-01 11:13:04
【问题描述】:
我想做的是每 30 分钟运行一次这个机器人。它的作用是截取屏幕截图,以便我查看内容。我尝试使用
thread.sleep();
但这对我不起作用。我将它设置为一个小的间隔,看看它是否真的可以工作,它只运行一次然后停止。我对 Java 很陌生,但我没有过多地使用 Java 机器人类或任何类型的循环。这是我的课...
public class usiwa{
public static void main(String[] args) throws AWTException, IOException, InterruptedException{
Robot bot = new Robot();
Date date = new Date();
Random ra = new Random();
int x0 = MouseInfo.getPointerInfo().getLocation().x;
int y0 = MouseInfo.getPointerInfo().getLocation().y;
int x1 = ra.nextInt(1302 - 1224 + 1) + 1224;
int y1 = ra.nextInt(80 - 70 + 1) + 70;
int dx = x1 - x0;
int dy = y1 - y0;
// time in msecs
int t = 1000;
int res = Math.max(dx, dy);
if(res > t) res = t;
int d = t/res;
float inv = (float) 1/(res - 1);
float a = 0;
long s = 0;
long e = 0;
s = System.currentTimeMillis();
for(int i = 0; i < res; i++) {
a += inv;
bot.mouseMove(x0 + (int) (a*dx), y0 + (int) (a*dy));
bot.delay(d);
}
e = System.currentTimeMillis();
System.out.println("Total time: " + (float) (e - s)/1000);
bot.mousePress(InputEvent.BUTTON1_MASK);
bot.mouseRelease(InputEvent.BUTTON1_MASK);
bot.delay(3000);
Rectangle r = new Rectangle(0, 0, 1000, 1000);
BufferedImage p = bot.createScreenCapture(r);
DateFormat dateFormat = new SimpleDateFormat("MM_dd_yyyy-hh.mm.ss a");
ImageIO.write(p, "png" , new File("C:/Users/Kalob_2/Desktop/Tracker/" + dateFormat.format(date) + ".png"));
}
}
我想做的就是让上述所有动作每 30 分钟重复一次。
感谢您的帮助。
【问题讨论】:
-
Java 这里有几个实用程序,最灵活的可能是ScheduledExecutorService