Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.8 kB
16
Indexable
Never
import javax.swing.*;
import java.awt.*;

public class ColorPicker extends Frame {
    private static final long serialVersionUID = 1L;

    // The current color of the window
    private Color currentColor = Color.BLACK;

    public ColorPicker() {
        setTitle("Color Picker");
        setSize(300, 300);

        // 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());
                    // 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);
    }
}