Results 1 to 12 of 12
  1. #1
    protocos is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default Help with Robot Class

    I've been trying to write a program to scan the screen for a certain pixel color and return the coordinates for that pixel. IT TAKES FOR-EVVEERRR. does anyone know of a way to 1. either speed this up, or 2. find a new method.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    It would probably be faster to get an image of the screen using the Robot, and then scan each pixel of the image.

  3. #3
    protocos is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default

    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?

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    I know that the Robot class has a screenshot method
    Well, what Object gets returned from that method?

  5. #5
    protocos is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default 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.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    I figured out my problem ... Solution: Use the BufferedImage Class
    Your welcome.

    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.

  7. #7
    protocos is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default

    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.
    I know it's technically inefficient but if your like me, it's 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
    }

  8. #8
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    but if your like me, it's easier to read.
    No, I actually believe in writing code that is easy to read and your code is difficult 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.

  9. #9
    protocos is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default

    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. #10
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    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.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default

    edit Misread something, sorry.

    db
    Last edited by DarrylBurke; 10-04-2009 at 09:26 PM.

  12. #12
    protocos is offline Member
    Join Date
    Feb 2009
    Posts
    8
    Rep Power
    0

    Default

    Now your test is simply:

    if (bi.getRGB(x, y) == color.getRGB())
    Ahh see now that makes a little more sense to me... (not being a professional when it comes to java)

    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

  1. Karel the robot
    By vulerious in forum New To Java
    Replies: 0
    Last Post: 07-31-2009, 09:48 PM
  2. Help with Java Robot and Runtime Class
    By Rmond1254 in forum New To Java
    Replies: 1
    Last Post: 02-18-2009, 06:33 AM
  3. Newbie - Robot Class
    By drasgear in forum New To Java
    Replies: 2
    Last Post: 11-14-2008, 04:46 PM
  4. Robot Class
    By Jessie Madman in forum New To Java
    Replies: 3
    Last Post: 05-15-2008, 04:13 AM
  5. Robot Class
    By jfredrickson in forum New To Java
    Replies: 2
    Last Post: 07-11-2007, 09:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •