The example here show us how to get the color of a pixel in the screen. We use the Robot.getPixelColor(int x, int y)
method to obtain the Color
of the pixel.
package org.kodejava.awt;
import java.awt.Color;
import java.awt.Robot;
import java.awt.AWTException;
public class ColorPickerDemo {
public static void main(String[] args) {
try {
Robot robot = new Robot();
// The pixel color information at 20, 20
Color color = robot.getPixelColor(200, 200);
// Print the RGB information of the pixel color
System.out.println("Red = " + color.getRed());
System.out.println("Green = " + color.getGreen());
System.out.println("Blue = " + color.getBlue());
} catch (AWTException e) {
e.printStackTrace();
}
}
}
Example output of code snippet above:
Red = 35
Green = 38
Blue = 53
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024