我想要做的是每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分钟重复一次。
感谢您的帮助。
答案 0 :(得分:1)
以下是重复任务的方法。在while循环中包装现有代码:
while(true) {
// do something
Thread.sleep(1000); // 1 second, the parameter is miliseconds
}
或者,创建一个cron作业来运行您的代码。以下将每30分钟重复一次。
30 * * * *
答案 1 :(得分:0)
答案 2 :(得分:0)
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();
while (true) {
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"));
}
}
}