Java 从鼠标光标下获取RGB值

Java 从鼠标光标下获取RGB值,java,swing,awt,cursor-position,mixing,Java,Swing,Awt,Cursor Position,Mixing,我试图建立一个程序,检测鼠标光标下的颜色,然后在屏幕上的窗口中显示颜色和RGB值。我对Java非常陌生,所以什么都不知道。在朋友的帮助下,我编写了两个代码,第一个代码获取缓冲图像特定坐标的RGB值,另一个代码获取用户定义的RGB值并显示一个包含颜色的窗格。我的问题是“如何让程序检测鼠标光标下的颜色,无论它在滚动什么?” public class Buffered_Image { public static void main(String[] args) throws IOException

我试图建立一个程序,检测鼠标光标下的颜色,然后在屏幕上的窗口中显示颜色和RGB值。我对Java非常陌生,所以什么都不知道。在朋友的帮助下,我编写了两个代码,第一个代码获取缓冲图像特定坐标的RGB值,另一个代码获取用户定义的RGB值并显示一个包含颜色的窗格。我的问题是“如何让程序检测鼠标光标下的颜色,无论它在滚动什么?”

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);
}
}

正如@Hovercraft所指出的那样

首先看一看

您需要知道鼠标光标的位置,虽然没有“简单”的方法来跟踪光标,但您可以使用

用示例更新

这是这个概念的一个小例子。它是基于鼠标光标的移动而工作的。一个可能的增强是当光标下的颜色发生变化时也会通知监视器侦听器

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());
                    }
                }

            }
        }
    }
}

您想知道应用程序或整个系统上下文中的颜色吗?@MadProgrammer-当程序开始运行时,需要有一个弹出的窗格,在该窗格中,我需要能够看到RGB值和光标正下方的颜色示例。无论发生什么情况,该窗格都应该能够显示这一点在屏幕上或其他正在运行的程序中。具有
getPixelColor(…)
。我会尝试使用它。@HovercraftFullOfEels Aww,然后我将在当前鼠标位置抓取1x1的屏幕截图:P@DavidKroukamp嘘,我做了一个测试示例,效果非常好;)@DavidKroukamp小心你的要求;)@MadProgrammer嘿,伙计们,我不确定是否允许我这样做,但我“回答“我自己的问题,因为我不知道有任何其他方法可以让我的新代码进入这个问题,因为注释块太小。请检查它,让我知道你是否可以在这个项目上帮助我。谢谢你。”