Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: help with java color code

  1. #1
    Junior Member Bridget's Avatar
    Join Date
    Sep 2018
    Location
    Lakeville, MN
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with java color code

    So I am very new to java in general, but I have this code here:
    import java.awt.Color;
    import java.awt.Rectangle;
    import java.awt.Robot;
    import java.awt.image.BufferedImage;
     
    public class FindColor
    {
        private static Rectangle rectangle = new Rectangle(800, 600);
     
        public static void main(String[] args) throws Exception
        {
            Robot r = new Robot();
            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.BLACK.getRGB())
                    {
                        r.mouseMove(x, y);
                        System.out.println("Found!");
                        break search;
                    }
                }
            }
        }
    }



    and it does exactly what it's supposed to do, however, let's say I already have the RGB values of a color I want to search for, what do I modify to make that work? I know it seems so simple but I get confused with the part that shows this:
      if(image.getRGB(x, y) == Color.BLACK.getRGB())


    AND I want the code to basically be something like this:
      if(image.getRGB(x, y) == MYRGBVALUES) {
    do blank;
    }

    Basically want I am trying to do here is.. instead of it searching for the built in color.black.getrgb, I want it to search for the 3 rgb values(ex. 155, 112, 124) that I already have in my notes.
    Last edited by Bridget; September 18th, 2018 at 08:21 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: help with java color code

    The BufferedImage class's getRGB method returns an int array. So your problem is how to compare the contents of two arrays. Look at the Arrays class for a method to compare the contents of two arrays.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member Bridget's Avatar
    Join Date
    Sep 2018
    Location
    Lakeville, MN
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with java color code

    Quote Originally Posted by Norm View Post
    getRGB returns an int array. So your problem is how to compare the contents of two arrays. Have you looked at that?
    Thank you for the fast reply, let me also add that I had a lot of help with making this code, so I am not exactly sure what you just said. (I apologize for my lack of knowledge, it's embarassing)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: help with java color code

    Look at the API doc for the BufferedImage class's getRGB method to see what it returns.

    Build an array with the values you want to compare against.

    Then look at the Arrays class's methods for comparing the contents of arrays.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member Bridget's Avatar
    Join Date
    Sep 2018
    Location
    Lakeville, MN
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with java color code

    Quote Originally Posted by Norm View Post
    Look at the API doc for the BufferedImage class's getRGB method to see what it returns.

    Build an array with the values you want to compare against.

    Then look at the Arrays class's methods for comparing the contents of arrays.
    Are you sure that is the route I want to go? I want a code that is similar to getRGB except some kind of code to where I would do:
    if(image.ContainsTheseRGBValues(122, 113, 144)) {

    I guess that might explain better what I am trying to do.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: help with java color code

    You could write a method that takes the image and the RGB values. It would search through all the pixels in the image looking for matches with the RGB values passed to it. I'm not sure what the method would return. You need to describe what you want it to do if if finds a match or not.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: help with java color code

    The BufferedImage class has two getRGB() methods. The one getRGB(x,y) returns an int in the form of an RGB value. Comparing ints is more straightforward than comparing arrays so I would stick with that. The Color class provides a constructor to convert R,G, and B values to an int. Or you can employ a simple shift and or construct. As Norm says, passing an array of RGB values and the image would be one way to go. But scanning an image over and over again is not very efficient. Once you get it working you may want to develop an algorithm to speed up the search (say by doing some sort of pre-processing on the image data that better lends itself to searching). It all depends on your requirements.

    Regards,
    Jim

Similar Threads

  1. need java code for Wavelet-based Color Histogram,please help
    By vchandra in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 25th, 2014, 04:23 AM
  2. Replies: 6
    Last Post: September 15th, 2013, 07:18 PM
  3. Why does the Color class have two variables for each color?
    By Melawe in forum Java Theory & Questions
    Replies: 5
    Last Post: May 10th, 2012, 04:21 PM
  4. Store HexDecimal Color Code in .properties file?
    By techwiz24 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 27th, 2011, 08:45 PM
  5. text color changer based on words/ word classification by color
    By knoxy5467 in forum Java Theory & Questions
    Replies: 25
    Last Post: June 15th, 2011, 07:52 AM