Jump to content

Color Finder Help Please.

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Skel

Skel

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Ive tried to make a color finder. Its supposed to find a color on your screen, then tell you that it's found it.

This is that i have so far.

[HIGHLIGHT="Java"]import java.awt.*;
import java.awt.Color.*;
import java.awt.image.BufferedImage;

public class ColorFinder {

public static void main (String args[]) {

Color color = new Color(255,255,255);

}

public boolean findColor(Color color, int red, int blue, int green) {

boolean found = false;

try {
Robot r = new Robot();
BufferedImage screenImg = r.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
for(int x = 0;x < screenImg.getWidth();x++) {
for(int y = 0;y < screenImg.getHeight();y++) {
if(color.getRGB() == screenImg.getRGB(x,y)) {
System.out.println("Found");
found = true;
}


}
}
} catch(Exception ex) {
}
return found;
}
}[/HIGHLIGHT]

I have no idea why it doesn't work. Could someone fix it & explain what they changed.

Thanks.

#2
kmlittle

kmlittle

    Newbie

  • Members
  • Pip
  • 1 posts
The code you wrote would not work as you wrote it. First, you need to write values all within the same scope. Since you put Color in a static method, the rest of the program would be unable to interact with it. Then you're going to use the main method to create an instance of an object which will then call the method findColor().

You were on the right track as far as what you wrote for your code, you just had things in the wrong places and never called the method. The main is what will be what is used to run through the program when you compile it.

I hope this helps on some level. I'm not an expert at programming but, I understand it enough to try and help I believe. I may have used incorrect terminology somewhere and I apoligize if I did.

import java.awt.*;

import java.awt.Color.*;

import java.awt.image.BufferedImage;


public class ColorFinder {

	public static void main (String args[]) {

	

		//Create the object finder from the ColorFinder class

		ColorFinder finder = new ColorFinder();

		

		//Use the finder object to call the method findColor()

		finder.findColor();

	}


	public boolean findColor() {

		boolean found = false;

		

		//myColor will be set to the value of 0,0,0 which I believe is black

		Color myColor = new Color(0,0,0);


		try {

			//no idea what the robot is for

			Robot r = new Robot();

			

			//capture the screen

			BufferedImage screenImg = r.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));

			

			//loop - look for a match at an x and y coordinate for the color we search for

			for(int x = 0;x < screenImg.getWidth();x++) {

				for(int y = 0;y < screenImg.getHeight();y++) {

					

					//If you find the color "black" then display the x and y coordinates

					//If it does not find the color "black" then print "No Match"

					if(myColor.getRGB() == screenImg.getRGB(x,y)) {

						System.out.println("Coordinates: " + x + " | " + y);

						found = true;

					} else {

						System.out.println("No Match");

					}

				} 

			}

		} catch(Exception ex) {


		}

		return found;

	}


}