当前位置: 首页 > 面试题库 >

从鼠标光标下获取RGB值

吕志诚
2023-03-14
问题内容

我正在尝试构建一个程序,该程序检测鼠标光标下的颜色,然后在屏幕上的窗口中显示颜色和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指出的那样。

首先看Robot#getPixelColor

您将需要知道鼠标光标在哪里,虽然没有“简单”的方式来跟踪光标,但是您可以使用以下方法获取它的当前位置
MouseInfo#getPointerInfo

用示例更新

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

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

            }
        }
    }
}


 类似资料:
  • 问题内容: JFreeChart中是否有一种方法可以从ChartMouseEvent中确定鼠标悬停在绘图空间中的x,y坐标?我尝试使用域十字线值,但这似乎不准确并且滞后于实际的鼠标事件。 谢谢, 杰夫 问题答案: 鼠标坐标相对于ChartPanel,因此您需要对其进行转换:

  • 我有一个子类JPanel的PointPanel,我想在其中实现以下行为:如果鼠标悬停在实例上,按下shift键,鼠标光标变为手光标;如果松开shift键,鼠标光标将变回默认光标。 为了实现这一点,我尝试在构造函数中添加一个: 这种方法不起作用。 包含此面板的窗口应该具有焦点,因为它是应用程序的唯一可见窗口。 我错过了什么?

  • 问题内容: 根据这个(使用JavaScript在光标下找到一个单词d-under-cursor-using- javascript))链接我可以在鼠标[指针下得到一个单词。英语很好。我将其更改(对于阿拉伯语言) 但每个单词返回“ $ 1”。请帮忙! 问题答案: 您 需要 出现在原始正则表达式中的括号。在正则表达式中,括号形成一个“匹配组”,该替换组将替换字符串中的“ ”。 正则表达式中没有任何匹配

  • 本文向大家介绍使用js获取鼠标坐标相关面试题,主要包含被问及使用js获取鼠标坐标时的应答技巧和注意事项,需要的朋友参考一下

  • 问题内容: 斯威夫特新手。 我在执行一项琐碎的任务时遇到了麻烦。我要做的就是 按需 获取鼠标光标的x,y坐标。我宁可 不要 等到鼠标移动事件触发后再抓住指针的坐标。 将不胜感激! 问题答案: 您应该看看NSEvent方法mouseLocation 编辑/更新: Xcode 11•Swift 5.1 如果您希望在应用程序处于活动状态时监视任何窗口上的事件,则可以添加与mouseMoved掩码匹配的L

  • 我正在尝试突出显示由鼠标悬停的瓷砖。这是我的代码: 编辑: 光标对象不在鼠标下面,我如何修复和对齐它?