Java RGB Value code Robot Class(Java RGB值码自动机类)
本文介绍了Java RGB值码自动机类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的代码在这里:
package RGBValues;
import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;
public class RGBValues {
public static void main(String[] args) throws Exception {
PointerInfo pointer;
pointer = MouseInfo.getPointerInfo();
Point coord = pointer.getLocation();
Robot robot = new Robot();
robot.delay(2000);
while(true) {
coord = MouseInfo.getPointerInfo().getLocation()…
Color color = robot.getPixelColor((int)coord.getX(), (int)coord.getY());
if( color.getGreen() == 255 &&
color.getBlue() == 255 &&
color.getRed() == 255
) {
System.out.println("WHITE");
}
robot.delay(1000);
}
}
}
我被困在如何让鼠标指向屏幕上的任何位置时,它都会在指针下面显示该像素的RGB值。有没有人能帮忙,知道该怎么做?我对Java非常陌生,所以我不知道如何解决这个问题。
推荐答案
除了可能让线程运行得有点疯狂之外,我看不出您的代码不能工作的任何原因...
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class WhatsMyColor {
public static void main(String[] args) {
new WhatsMyColor();
}
public WhatsMyColor() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
Thread thread = new Thread(new BackgroundMonitor());
thread.setDaemon(true);
thread.start();
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Color color = getBackground();
String text = color.getRed() + "x" + color.getGreen() + "x" + color.getBlue();
FontMetrics fm = g.getFontMetrics();
int x = (getWidth() - fm.stringWidth(text)) / 2;
int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
g.drawString(text, x, y);
}
public class BackgroundMonitor implements Runnable {
@Override
public void run() {
try {
Robot robot = new Robot();
Color previous = null;
while (true) {
Point coord = MouseInfo.getPointerInfo().getLocation();
Color color = robot.getPixelColor((int) coord.getX(), (int) coord.getY());
if (previous == null || !previous.equals(color)) {
SwingUtilities.invokeLater(new UpdateBackgroud(color));
}
try {
Thread.sleep(250);
} catch (InterruptedException ex) {
}
}
} catch (AWTException | HeadlessException exp) {
exp.printStackTrace();
}
}
}
public class UpdateBackgroud implements Runnable {
private Color color;
public UpdateBackgroud(Color color) {
this.color = color;
}
@Override
public void run() {
setBackground(color);
}
}
}
}
这篇关于Java RGB值码自动机类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:Java RGB值码自动机类
基础教程推荐
猜你喜欢
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01