JAVA JFrames Dispay the color and RGB value of the backgroun
JAVA (JFrames)
Dispay the color and RGB value of the background wherever the mouse is on the screen.
Solution
//Move cursor for get the color value
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.PointerInfo;
import java.awt.Robot;
import java.io.IOException;
import static java.lang.Thread.MIN_PRIORITY;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import static java.lang.Thread.sleep;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class ColorFinder {
/**
*
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
new ColorFinder();
}
public ColorFinder() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
try {
JFrame frame = new JFrame(\"Color Finder\");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new MouseColorPane());
frame.setSize(600, 400);
frame.getContentPane().setBackground(Color.yellow);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (HeadlessException | AWTException exp) {
}
}
});
}
public class MouseColorPane extends JPanel implements MouseMonitorListener {
private final Robot robot;
private final JLabel label;
/**
*
* @throws AWTException
*/
public MouseColorPane() throws AWTException {
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.black));
label = new JLabel();
label.setOpaque(true);
label.setBackground(Color.WHITE);
panel.setBounds(250,50 , 200, 50);
panel.add(label);
add(panel);
robot = new Robot();
PointerInfo pi = MouseInfo.getPointerInfo();
updateColor(pi.getLocation());
MouseMonitor monitor = new MouseMonitor();
monitor.setMouseMonitorListener(this);
monitor.start();
}
/**
*
* @param p
*/
protected void updateColor(Point p) {
Color pixelColor = robot.getPixelColor(p.x, p.y);
// label.setText(p.x + \"x\" + p.y + \" = \" + pixelColor);
label.setText(\"Red:\"+pixelColor.getRed()+\" // \" + \"Green\" + pixelColor.getGreen()+\" // \" + \"Blue:\" + pixelColor.getBlue());
setBackground(pixelColor);
}
/**
*
* @param p
*/
@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());
}
}
}
}
}
}


