问题描述
我试图建立检测的颜色是鼠标光标下的程序,然后显示在屏幕上的一个窗口的颜色和RGB值。我很对Java所以不知道任何东西。我有两个codeS我已经从朋友制作,以帮助下,第一个获得一个特定的RGB值的缓冲图像的坐标,并且其它取用户定义的RGB值,并显示与颜色在一个窗格它。我的问题是我如何获得该程序检测到彩色鼠标光标下,不管它是什么在滚动?
公共类Buffered_Image
{
公共静态无效的主要(字串[] args)抛出IOException异常
{
BufferedImage的BI = ImageIO.read(新文件(C:/Users/user/Pictures/Hornet.jpg));
颜色C =新的色彩(bi.getRGB(50,40));
INT红色= c.getRed();
INT绿色= c.getGreen();
INT蓝= c.getBlue(); System.out.print(红色+红+绿色+绿色+蓝色+蓝+\\ n);
}
}
公共类RGB_Pane
{公共静态无效的主要(字串[] args)
{
JFrame的F =新的JFrame(RGB);
面板潘=新面板();
F.getContentPane()加(PAN)。
F.pack();
F.setVisible(真);
F.setSize(300,300);
}
}类面板继承JPanel
{
公共面板()
{
集preferredSize(新尺寸(200,200));
INT红色=的Integer.parseInt(JOptionPane.showInputDialog(RED为输入值));
INT绿色=的Integer.parseInt(JOptionPane.showInputDialog(绿色输入数值));
INT蓝色=的Integer.parseInt(JOptionPane.showInputDialog(蓝色输入值));
颜色Defined_Color =新的色彩(红,绿,蓝);
的setBackground(Defined_Color);
}
}
由于@Hovercraft指出。
通过看的。
您需要知道鼠标的光标,而没有轻松的方式来跟踪光标,你可以得到它使用的
与例如更新时间:
这是概念的小例子。这部作品以鼠标光标的moition。一个可能的增强将通知还监视监听器当光标下的颜色变化,以及...
公共类WhatsMyColor { 公共静态无效的主要(字串[] args)抛出IOException
新WhatsMyColor();
} 公共WhatsMyColor(){
EventQueue.invokeLater(新的Runnable(){
@覆盖
公共无效的run(){
尝试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}赶上(ClassNotFoundException的前){
}赶上(InstantiationException前){
}赶上(IllegalAccessException前){
}赶上(UnsupportedLookAndFeelException前){
} 尝试{
JFrame的帧=新的JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
frame.add(新MouseColorPane());
frame.setSize(400,200);
frame.setLocationRelativeTo(NULL);
frame.setVisible(真);
}赶上(例外EXP){
exp.printStackTrace();
} }
});
} 公共类MouseColorPane继承JPanel实现MouseMonitorListener { 私人机器人机器人; 私人标签的JLabel; 公共MouseColorPane()抛出的AWTException { 标签=新的JLabel(); 的setLayout(新的GridBagLayout());
添加(标签); 机器人=新机器人();
PointerInfo PI = MouseInfo.getPointerInfo();
updateColor(pi.getLocation());
MouseMonitor显示器=新MouseMonitor();
monitor.setMouseMonitorListener(本);
monitor.start(); } 保护无效updateColor(点p){ 颜色pixelColor = robot.getPixelColor(p.x,p.y);
的setBackground(pixelColor); label.setText(p.x +×+ p.y +=+ pixelColor); } @覆盖
公共无效mousePositionChanged(最终点p){
SwingUtilities.invokeLater(Runnable的新(){ @覆盖
公共无效的run(){
updateColor(P);
} });
}
} 公共接口MouseMonitorListener { 公共无效mousePositionChanged(点P);
} 公共静态类MouseMonitor继承Thread { 私人点lastPoint;
私人MouseMonitorListener侦听器; 公共MouseMonitor(){
setDaemon(真);
setPriority(MIN_PRIORITY);
} 公共无效setMouseMonitorListener(MouseMonitorListener监听){
this.listener =侦听器;
} 公共MouseMonitorListener getMouseMonitorListener(){
返回侦听器;
} 保护点getMouseCursorPoint(){
PointerInfo PI = MouseInfo.getPointerInfo();
返回pi.getLocation();
} @覆盖
公共无效的run(){
lastPoint = getMouseCursorPoint();
而(真){
尝试{
睡眠(250);
}赶上(InterruptedException的前){
} 点currentPoint = getMouseCursorPoint();
如果(!currentPoint.equals(lastPoint)){
lastPoint = currentPoint;
MouseMonitorListener听者= getMouseMonitorListener();
如果(听众!= NULL){
listener.mousePositionChanged((点)lastPoint.clone());
}
} }
}
}
}
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 + "\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);
}
}
As @Hovercraft has pointed out.
Start by looking at Robot#getPixelColor
.
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
UPDATED with example
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值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!