Guest User

Untitled

a guest
Jan 3rd, 2023
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.io.Serial;
  4.  
  5. public class ColorPicker extends JFrame {
  6. @Serial
  7. private static final long serialVersionUID = 1L;
  8.  
  9. JLabel colorValues = new JLabel();
  10.  
  11.  
  12. // The current color of the window
  13. private Color currentColor = Color.BLACK;
  14.  
  15. public ColorPicker() {
  16. setTitle("Color Picker");
  17. setSize(300, 300);
  18. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  19.  
  20. colorValues.setBounds(0, 250, 300 , 50);
  21. colorValues.setVisible(true);
  22.  
  23. add(colorValues);
  24.  
  25. // Create a new Robot object
  26. final Robot robot;
  27. try {
  28. robot = new Robot();
  29. } catch (AWTException e) {
  30. e.printStackTrace();
  31. return;
  32. }
  33.  
  34. // Create a new thread to continuously update the window
  35. Thread updateThread = new Thread(new Runnable() {
  36. public void run() {
  37. while (true) {
  38. // Get the mouse position relative to the screen
  39. Point mousePos = MouseInfo.getPointerInfo().getLocation();
  40. // Get the color of the pixel under the mouse
  41. currentColor = robot.getPixelColor((int) mousePos.getX(), (int) mousePos.getY());
  42.  
  43. String color = Integer.toString(currentColor.getRGB());
  44. colorValues.setText(color);
  45.  
  46. // Update the window with the new color
  47. repaint();
  48.  
  49. // Sleep for a short time before checking the mouse position again
  50. try {
  51. Thread.sleep(10);
  52. } catch (InterruptedException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. }
  57. });
  58. // Start the update thread
  59. updateThread.start();
  60.  
  61.  
  62. }
  63.  
  64. public void paint(Graphics g) {
  65. // Set the background color of the window to the current color
  66. setBackground(currentColor);
  67.  
  68. }
  69.  
  70. public static void main(String[] args) {
  71.  
  72. new ColorPicker().setVisible(true);
  73.  
  74. }
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment