问题描述
我正在尝试构建一个程序来检测鼠标光标下的颜色,然后在屏幕上的窗口中显示颜色和 RGB 值.我对 Java 很陌生,所以什么都不知道.我有两个代码,在朋友的帮助下,第一个获取缓冲图像的特定坐标的 RGB 值,另一个获取用户定义的 RGB 值并显示带有颜色的窗格.我的问题是如何让程序检测鼠标光标下的颜色,无论它滚动的是什么?
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 + "
" );
}
}
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 指出的那样.
As @Hovercraft has pointed out.
首先查看 Robot#getPixelColor
.
您需要知道鼠标光标的位置,虽然没有简单"的方法来跟踪光标,但您可以使用 MouseInfo#getPointerInfo
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
更新示例
这是这个概念的一个小例子.这基于鼠标光标的运动.一个可能的改进是当光标下的颜色也发生变化时也通知监视器侦听器......
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());
}
}
}
}
}
}
这篇关于从鼠标光标下获取RGB值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!