java 获取光标_java – 从鼠标光标下获取RGB值

正如@Hovercraft指出的那样.

您将需要知道鼠标光标的位置,而没有“简单”的方法来跟踪光标,您可以使用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());

}

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值