You can use the Robot
class (see Documentation) to get the colour for a set of coordinates relative to the screen or the GraphicsDevice
you want:
public Color getPixelColor(int x, int y) throws AWTException {
Robot robot = new Robot();
return robot.getPixelColor(x, y);
}
You can then retrieve the RGB values from that returned Color
object. Make sure your coordinates are good though!
As an additional test, you can try running the following, which displays the pointer's pointed colour relative to the screen (in other words, the absolute cursor coordinates) every second:
import java.awt.AWTException;
import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
public class Test {
public static void main(String[] args) throws Exception {
while (true) {
try {
System.out.println(getPointerColor());
Thread.sleep(1000);
} catch (AWTException awte) {
System.out.println("Error while getting pointer's color!");
} catch (InterruptedException ie) {
System.out.println("Error while sleeping!");
}
}
}
public static Color getPointerColor() throws AWTException {
Point coordinates = MouseInfo.getPointerInfo().getLocation();
Robot robot = new Robot();
return robot.getPixelColor((int) coordinates.getX(), (int) coordinates.getX());
}
}