Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.io.Serial;
- public class ColorPicker extends JFrame {
- @Serial
- private static final long serialVersionUID = 1L;
- JLabel colorValues = new JLabel();
- // The current color of the window
- private Color currentColor = Color.BLACK;
- public ColorPicker() {
- setTitle("Color Picker");
- setSize(300, 300);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- colorValues.setBounds(0, 250, 300 , 50);
- colorValues.setVisible(true);
- add(colorValues);
- // Create a new Robot object
- final Robot robot;
- try {
- robot = new Robot();
- } catch (AWTException e) {
- e.printStackTrace();
- return;
- }
- // Create a new thread to continuously update the window
- Thread updateThread = new Thread(new Runnable() {
- public void run() {
- while (true) {
- // Get the mouse position relative to the screen
- Point mousePos = MouseInfo.getPointerInfo().getLocation();
- // Get the color of the pixel under the mouse
- currentColor = robot.getPixelColor((int) mousePos.getX(), (int) mousePos.getY());
- String color = Integer.toString(currentColor.getRGB());
- colorValues.setText(color);
- // Update the window with the new color
- repaint();
- // Sleep for a short time before checking the mouse position again
- try {
- Thread.sleep(10);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- });
- // Start the update thread
- updateThread.start();
- }
- public void paint(Graphics g) {
- // Set the background color of the window to the current color
- setBackground(currentColor);
- }
- public static void main(String[] args) {
- new ColorPicker().setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment