Getting RGB value from under mouse cursor

As @Hovercraft has pointed out.

Start by looking at Robot#getPixelColor.

You will need to know where the mouse cursor is, while there’s no “easy” way to track the cursor, you can get it’s current location using MouseInfo#getPointerInfo

UPDATED with example

This is little example of the concept. This works based on the moition of the mouse cursor. A possible enhancement would be to also notify the monitor listener when the color changes under the cursor as well…

public class WhatsMyColor {

    public static void main(String[] args) throws IOException {
        new WhatsMyColor();
    }

    public WhatsMyColor() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                try {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new MouseColorPane());
                    frame.setSize(400, 200);
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (Exception exp) {
                    exp.printStackTrace();
                }

            }
        });
    }

    public class MouseColorPane extends JPanel implements MouseMonitorListener {

        private Robot robot;

        private JLabel label;

        public MouseColorPane() throws AWTException {

            label = new JLabel();

            setLayout(new GridBagLayout());
            add(label);

            robot = new Robot();
            PointerInfo pi = MouseInfo.getPointerInfo();
            updateColor(pi.getLocation());
            MouseMonitor monitor = new MouseMonitor();
            monitor.setMouseMonitorListener(this);
            monitor.start();

        }

        protected void updateColor(Point p) {

            Color pixelColor = robot.getPixelColor(p.x, p.y);
            setBackground(pixelColor);

            label.setText(p.x + "x" + p.y + " = " + pixelColor);

        }

        @Override
        public void mousePositionChanged(final Point p) {
            SwingUtilities.invokeLater(new Runnable() {

                @Override
                public void run() {
                    updateColor(p);
                }

            });
        }
    }

    public interface MouseMonitorListener {

        public void mousePositionChanged(Point p);
    }

    public static class MouseMonitor extends Thread {

        private Point lastPoint;
        private MouseMonitorListener listener;

        public MouseMonitor() {
            setDaemon(true);
            setPriority(MIN_PRIORITY);
        }

        public void setMouseMonitorListener(MouseMonitorListener listener) {
            this.listener = listener;
        }

        public MouseMonitorListener getMouseMonitorListener() {
            return listener;
        }

        protected Point getMouseCursorPoint() {
            PointerInfo pi = MouseInfo.getPointerInfo();
            return pi.getLocation();
        }

        @Override
        public void run() {
            lastPoint = getMouseCursorPoint();
            while (true) {
                try {
                    sleep(250);
                } catch (InterruptedException ex) {
                }

                Point currentPoint = getMouseCursorPoint();
                if (!currentPoint.equals(lastPoint)) {
                    lastPoint = currentPoint;
                    MouseMonitorListener listener = getMouseMonitorListener();
                    if (listener != null) {
                        listener.mousePositionChanged((Point) lastPoint.clone());
                    }
                }

            }
        }
    }
}

Leave a Comment