栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从鼠标光标下获取RGB值

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从鼠标光标下获取RGB值

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/485841.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号