Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to get color of a Screen through Robot class in Java ?

A simple program to demonstrate capturing of pixel colors of screen in Java

package com.hubberspot.example;

import java.awt.AWTException;
import java.awt.Color;
import java.awt.Robot;

public class RobotColorDemo {
 
   public static void main(String[] args) {
  
      try {
 // Create a Robot object
        Robot robot = new Robot();

        // Robot class has method getPixelColor which takes
        // two parameters as (x,y) x for X position of pixel
        // and y for Y position of pixel and it returns back  
        // refernce to object of Color which is stored in color         
            
        Color color = robot.getPixelColor(100, 100);

        // Color class has methods such as getRed() , getGreen() 
        // and getGreen() and getBlue() method which prints RGB 
        //  information of the pixel color
            
        System.out.println("Red color of pixel   = " + color.getRed());
        System.out.println("Green color of pixel = " + color.getGreen());
        System.out.println("Blue color of pixel  = " + color.getBlue());

     } 
     catch (AWTException e) {
        e.printStackTrace();
     }

   }

}




Output of the program :




 
© 2021 Learn Java by Examples Template by Hubberspot