Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8743349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:39:37+00:00 2026-06-13T11:39:37+00:00

I am trying to build a program that detects the color that is under

  • 0

I am trying to build a program that detects the color that is under the mouse cursor and then displays the color and RGB values in a window on the screen. I am VERY new to Java so do not know much of anything. I have two codes I have worked on, with help from a friend, The first one gets the RGB values of a specific coordinate of a buffered image, and the other takes user defined RGB values and shows a pane with the color in it. My question is “how do I get the program to detect the color under the mouse cursor no matter what it is scrolling over?

public class Buffered_Image 
{
public static void main(String[] args) throws IOException 
{
    BufferedImage bi = ImageIO.read(new File("C:/Users/user/Pictures/Hornet.jpg"));
    Color c = new Color(bi.getRGB(50,40));
    int red=c.getRed();
    int green=c.getGreen();
    int blue=c.getBlue();

    System.out.print("Red " + red + " Green " + green+ " Blue" + blue + "\n" );
}
}




public class RGB_Pane 
{

public static void main(String[] args) 
{
    JFrame F = new JFrame("RGB");
    Panel Pan = new Panel();
    F.getContentPane().add(Pan);
    F.pack();
    F.setVisible(true);
    F.setSize(300, 300);
}
}

class Panel extends JPanel
{
public Panel()
{ 
    setPreferredSize(new Dimension(200,200));
    int Red = Integer.parseInt(JOptionPane.showInputDialog("Enter value for RED"));
    int Green = Integer.parseInt(JOptionPane.showInputDialog("Enter value for Green"));
    int Blue = Integer.parseInt(JOptionPane.showInputDialog("Enter value for BLUE"));
    Color Defined_Color = new Color(Red,Green,Blue);
    setBackground(Defined_Color);
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T11:39:38+00:00Added an answer on June 13, 2026 at 11:39 am

    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());
                        }
                    }
    
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build some program that detects if a server of some
I'm trying to build a c++ program that customizes another window title bar, adding
I am trying to build a program that accepts an array of integers as
I'm trying to build a small program that hosts vst effects and I would
When trying to build a simple test program that uses atomic operations, I get
I was trying to build a very simple program in C that returns a
I was trying to build a program that puts a radical into simplest radical
I am trying to build a program that uses libusb and I get a
I am trying to build a program that, once the button was click, every
I'm trying to build a program that goes like this: User enters 3 numbers.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.