Results 1 to 12 of 12
Thread: Help with Robot Class
- 09-30-2009, 02:27 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
- 09-30-2009, 02:34 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
It would probably be faster to get an image of the screen using the Robot, and then scan each pixel of the image.
- 10-01-2009, 04:03 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
In concept, that seems like it would work...
Now how exactly do you do that.
I know that the Robot class has a screenshot method however I do not know how to make an object out of the screenshot and search for a pixel in that object.
Any suggestions on where to start?
- 10-01-2009, 04:58 AM #4
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Well, what Object gets returned from that method?I know that the Robot class has a screenshot method
- 10-02-2009, 06:23 AM #5
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
Solution
I figured out my problem and here's my advice in case anyone wonders on this topic having the same problem. For some strange reason, the Robot class creates a new screenshot every time you use the robot.getPixelColor(x,y); method making an imbedded for loop take almost an hour to scan the entire screen.
Solution: Use the BufferedImage Class
Java Code:import java.awt.*; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.awt.Dimension; public class PixelScan { public static void main (String[] args)throws AWTException { initiate(); } static Robot r; static Toolkit toolkit = Toolkit.getDefaultToolkit(); static Dimension scrn = toolkit.getScreenSize(); static BufferedImage bi; static void initiate()throws AWTException { r=new Robot(); bi=r.createScreenCapture(new Rectangle(scrn.width,scrn.height)); for(int x=0;x<ff.scrn.width;x++) { for(int y=0;y<ff.scrn.height;y++) { if(color(x,y).equals("58,145,72"))//127,194,65 being the RGB of a color { //code to execute if the pixel is found System.out.println("x="+x+",y="+y); r.mouseMove(x,y); } } } } static String color(int x, int y) { int r=(bi.getRGB(x, y) & 0xFF0000)>>16; int g=(bi.getRGB(x, y) & 0xFF00)>>8; int b=(bi.getRGB(x, y) & 0xFF); return r+","+g+","+b; } }Last edited by protocos; 10-03-2009 at 06:50 AM.
- 10-02-2009, 06:53 AM #6
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Your welcome.I figured out my problem ... Solution: Use the BufferedImage Class
Of course your color(...) method is extremely inefficient. Creating and comparing Strings is not a very good solution when you can just compare the int value of the Color and pixel.
And Use the "Code" tags when posting code. Formatted code is much easier to read.
- 10-03-2009, 06:49 AM #7
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
I know it's technically inefficient but if your like me, it's easier to read.Of course your color(...) method is extremely inefficient. Creating and comparing Strings is not a very good solution when you can just compare the int value of the Color and pixel.
And Use the "Code" tags when posting code. Formatted code is much easier to read.
But if your one of those people who operates as efficiently as possible, the go for it,
if(bi.getRGB(x, y)==number)
{
//code to execute if the pixel is found
}
- 10-03-2009, 07:04 AM #8
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
No, I actually believe in writing code that is easy to read and your code is difficult to read.but if your like me, it's easier to read.
Everybody understands that:
if (bi.getRGB(x, y) == Color.getRGB())
is simply comparing two int values.
On the other hand your code is converting a single int value to a String containing 3 pieces of information. So not only is the code inefficient, it is confusing as there is no reason the separate the int value into 3 components.
- 10-03-2009, 03:36 PM #9
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
But if your like me, RGB easier to read.
... and since I'm initially getting the colors via the manual way, robot.getPixelColor(x,y); and writing it down, it is a whole lot easier for me to just type in the Red Green and Blue components without having to worry about an integer value.
- 10-04-2009, 01:32 AM #10
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
You completely miss the point. Outside the loop ou create a Color object as follows:
Color color = new Color(58, 145, 72);
Now your test is simply:
if (bi.getRGB(x, y) == color.getRGB())
There is no need to convert anything to an int value. It is far easier to understand than adding all the code that does the bitwise shifting to get each individual color so you can create a string.
For anybody else who is just learning programming your solution is far to complicated and far too difficult to read and understand because all the code is completely unnecessary. Learn how to take advantage of the API.
- 10-04-2009, 08:55 PM #11
edit Misread something, sorry.
dbLast edited by DarrylBurke; 10-04-2009 at 09:26 PM.
- 10-05-2009, 08:15 PM #12
Member
- Join Date
- Feb 2009
- Posts
- 8
- Rep Power
- 0
Ahh see now that makes a little more sense to me... (not being a professional when it comes to java)Now your test is simply:
if (bi.getRGB(x, y) == color.getRGB())
revised Code
Java Code:import java.awt.*; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.awt.Dimension; public class PixelScan { static Robot r; static BufferedImage bi; static Toolkit toolkit = Toolkit.getDefaultToolkit(); static Dimension scrn = toolkit.getScreenSize(); public static void main (String[] args)throws AWTException { r=new Robot(); bi=r.createScreenCapture(new Rectangle(scrn.width,scrn.height)); Color c=new Color(153,153,153);//color that your searching for for(int x=0;x<ff.scrn.width;x++) { for(int y=0;y<ff.scrn.height;y++) { if(bi.getRGB(x, y)==c.getRGB()) { System.out.println("x="+x+",y="+y);//prints all coordinates that match the above specified color } } } } }
Similar Threads
-
Karel the robot
By vulerious in forum New To JavaReplies: 0Last Post: 07-31-2009, 09:48 PM -
Help with Java Robot and Runtime Class
By Rmond1254 in forum New To JavaReplies: 1Last Post: 02-18-2009, 06:33 AM -
Newbie - Robot Class
By drasgear in forum New To JavaReplies: 2Last Post: 11-14-2008, 04:46 PM -
Robot Class
By Jessie Madman in forum New To JavaReplies: 3Last Post: 05-15-2008, 04:13 AM -
Robot Class
By jfredrickson in forum New To JavaReplies: 2Last Post: 07-11-2007, 09:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks