-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColourPicker.java
68 lines (59 loc) · 2.42 KB
/
ColourPicker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* @author Steven Sams
* RGB Colour inspector desktop tool
*/
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.MouseInfo;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JWindow;
import javax.swing.SwingConstants;
public class ColourPicker {
private ColourPicker() {
Color colour;
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1));
panel.setLayout(new BorderLayout());
JLabel label = new JLabel();
label.setHorizontalTextPosition(SwingConstants.CENTER);
JWindow frame = new JWindow();
frame.setLayout(new BorderLayout());
frame.setPreferredSize(new Dimension(128, 128));
frame.setAlwaysOnTop(true);
frame.add(panel, BorderLayout.CENTER);
frame.add(label, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
try {
Robot robot = new Robot();
while (true) {
colour = robot.getPixelColor(MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y);
frame.setLocation(MouseInfo.getPointerInfo().getLocation().x + 10, MouseInfo.getPointerInfo().getLocation().y - 10);
BufferedImage image=robot.createScreenCapture(new Rectangle(MouseInfo.getPointerInfo().getLocation(), new Dimension(10,10)));
panel.removeAll();
panel.add(new JLabel(new ImageIcon(image.getScaledInstance(panel.getWidth(), panel.getHeight(), BufferedImage.SCALE_FAST))), BorderLayout.CENTER);
label.setText(colour.getRed() + ", " + colour.getGreen() + ", " + colour.getBlue());
panel.validate();
Thread.sleep(10);
}
} catch (InterruptedException ex) {
Logger.getLogger(ColourPicker.class.getName()).log(Level.SEVERE, null, ex);
} catch (AWTException ex) {
Logger.getLogger(ColourPicker.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void main(String args[]) {
ColourPicker picker = new ColourPicker();
}
}