简体   繁体   English

如何使Java程序不断检查是否有更改并循环进行直到出现

[英]How to make a Java program to constantly check if there has been changes and loop it until there is

I'm trying to figure out how to make my java program (code is below) to check after every 5 seconds if the color of the background has changed and then if it has changed then using java.awt.robot go to a certain area of screen and click. 我试图弄清楚如何使我的Java程序(下面的代码)每5秒检查一次背景颜色是否改变,然后是否改变,然后使用java.awt.robot转到某个区域屏幕并单击。

    Color queueColor1 = robot.getPixelColor(614, 756);
    TimeUnit.SECONDS.sleep(5);
    Color queueColor2 = robot.getPixelColor(614, 756);
    System.out.println(queueColor1);
    System.out.println(queueColor2);
    boolean readyCheck = (queueColor1 != queueColor2);
    if (readyCheck = false) {
        TimeUnit.SECONDS.sleep(5);

    } while (queueColor1 != queueColor2);
        robot.mouseMove(720, 629);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
        robot.mousePress(InputEvent.BUTTON1_MASK);
        robot.mouseRelease(InputEvent.BUTTON1_MASK);
}

I already had made a code where it does the same thing but it wasn't a loop. 我已经编写了一个代码,可以执行相同的操作,但这不是循环。 I had to copy the lines over and over again, till with TimeUnit.SECONDS.sleep(5); 我不得不一遍又一遍地复制行,直到使用TimeUnit.SECONDS.sleep(5); it was total of 2 minutes and I was replacing every "queueColor2" with "queueColor3.....16" or something. 总共花了2分钟,我将每个“ queueColor2”替换为“ queueColor3 ..... 16”之类的东西。 So it was comparing "queueColor1" with "queueColor3" and so on. 因此,它正在将“ queueColor1”与“ queueColor3”进行比较,依此类推。

Your code has a number of problems: 您的代码有很多问题:
- The statement (queueColor1 != queueColor2) always returns true since you're comparing the objects and not their contents. -由于比较对象而不是对象内容,因此语句(queueColor1 != queueColor2)始终返回true
- if (readyCheck = false) assigns false to the variable - if (readyCheck = false)false分配给变量
- your loop while (queueColor1 != queueColor2); -您的循环while (queueColor1 != queueColor2); is infinite (see above) 是无限的(见上文)

You might try something like this (it's just a hint; I didn't even check whether it compiles) 您可以尝试这样的操作(这只是一个提示;我什至没有检查它是否可以编译)

Color old_color = robot.getPixelColor(614, 756);
for (;;)
{
  TimeUnit.SECONDS.sleep(5);
  Color new_color = robot.getPixelColor(614, 756);
  if (!new_color.equals(old_color))
  {
    robot.mouseMove(720, 629);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);    
    old_color = new_color;
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM