Reputation: 13
Using these classes :
import java.awt.AWTException;
import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;
import javax.swing.JFrame;
How can i get my program to read the rgb values under my mouse while i hover over the screen and have a Jframe display the color itself. the rgb values. and possibly the name of the color
Upvotes: 1
Views: 1562
Reputation: 25028
package stack;
import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;
public class TheColorInfo {
static PointerInfo pointer;
static Point point;
static Robot robot;
static Color color;
public static void main(String[] args) {
try{
robot = new Robot();
while(true){
pointer = MouseInfo.getPointerInfo();
point = pointer.getLocation();
if(point.getX() == 0 && point.getY() == 0){
break; // stop the program when you go to (0,0)
}else{
color = robot.getPixelColor((int)point.getX(),(int)point.getY());
System.out.println("Color at: " + point.getX() + "," + point.getY() + " is: " + color);
}
}
}catch(Exception e){
}
}
}
The SSCCE above shows how you can go about getting the color of any pixel on the screen using the Robot
class.
Since the return type of getPixelColor()
is java.awt.Color
, you can extract the red, green and blue values of the pixel. I have left adding the GUI to you.
Upvotes: 2