用java响应颜色事件
我正在构建一个java应用程序来解决难题。我编码的方式基本上是程序将拍摄屏幕截图,在屏幕截图中找到一个像素,然后通过机器人功能将鼠标移动到桌面上的该位置。我理解拍摄屏幕截图、将其存储在数组中、探索数组直到弹出具有正确颜色组合的存储像素,并将鼠标移动到屏幕上的该位置的理论,但是我一生都无法得到代码下来。如果有人知道,或者可以组合一个拍摄屏幕截图的示例代码,将其存储在一个数组中(或者存储设备,我不知道数组是否最适合这种特定用途),从该数组中找到一个像素移动鼠标到像素位置,然后清除数组,我会非常高兴,因为这让我发疯!
到目前为止,我有:
public static void main(String[] args) throws Exception{
Robot robot = new Robot();
{
private static Rectangle rectangle = new Rectangle(0, 0, 1075, 700);
{
BufferedImage image = r.createScreenCapture(rectangle);
search: for(int x = 0; x < rectangle.getWidth(); x++)
{
for(int y = 0; y < rectangle.getHeight(); y++)
{
if(image.getRGB(x, y) == Color.getRGB(195, 174, 196))
{
Robot.mouseMove(x, y);
break search;
}
}
}
}
}
我收到三个错误:
表达式开始非法,指示器指向下面的代码段中的 get
私有静态矩形矩形=新矩形(Toolkit.getDefaultToolkit().getScreenSize());
表达式开始非法,指示器指向下面代码段中的 Size
私有静态矩形矩形=新矩形(Toolkit.getDefaultToolkit().getScreenSize());
;预期指标指向 Rectangle 矩形
私有静态矩形矩形=新矩形(Toolkit.getDefaultToolkit().getScreenSize());
I am building a java application to solve puzzles. The way i am coding it basically the program will take a screen shot, find a pixel in the screen shot, and move the mouse though the Robot function to that position on the desktop. I understand the theory behind taking a screen shot, storing it in an array, exploring the array until a stored pixel with the right color combo pops up, and moving mouse to that position on screen, however i cannot for the life of me get the code down. If anyone knows, or could knock together a sample code that takes a screen shot, stores it in an array (or and storage device i don't know if array is the best for this particular use) finds a pixel from that array moves mouse to pixel position and then clears array, I would be amazingly great-full because this is driving me nuts!
so far i have:
public static void main(String[] args) throws Exception{
Robot robot = new Robot();
{
private static Rectangle rectangle = new Rectangle(0, 0, 1075, 700);
{
BufferedImage image = r.createScreenCapture(rectangle);
search: for(int x = 0; x < rectangle.getWidth(); x++)
{
for(int y = 0; y < rectangle.getHeight(); y++)
{
if(image.getRGB(x, y) == Color.getRGB(195, 174, 196))
{
Robot.mouseMove(x, y);
break search;
}
}
}
}
}
i am getting three errors:
illegal start of expression, the indicator pointing at the get in code segment below
private static Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
illegal start of expression, the indicator pointing at the Size in code segment below
private static Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
; expected indicator pointing at Rectangle rectangle
private static Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建屏幕截图并循环播放并不难。
GraphicsDevice 的 Javadoc 将告诉您如何操作以获得正确的屏幕尺寸。
我认为你唯一不能做的就是响应“颜色事件”。您可以轮询屏幕以查看颜色何时发生变化。
-自从问题扩展以来进行编辑-
如果您要立即检查图像,则无需将图像写入磁盘。
BufferedImage 已经有办法访问各个像素,因此我认为不需要将像素数据转换为数组。
Creating the screen shot and looping though it is not that hard.
The Javadoc for the GraphicsDevice will tell you how to get the right screen size.
The only thing I don't think you can do is respond to "color events". You can poll the screen to see when the color has changed though.
-edit since the question was expanded-
You don't need to write the image out to disk if you are going to check it there and then.
The BufferedImage already has a way to access the individual pixels so I don't think there is a need to translate the pixel data into an array.