title: RGB Value Under Cursor anywhere on the screen
body: <p>My code will run and compile. It works if you are hovering anywhere inside of the JFrame, but when you get out into the screen, it will only work sometimes, and if it does, it will only take in the RGB value for white, and sometimes gray. I'm not sure why it does not work every time I go outside of the box, and why it does not get all RGB values outside of the frame, but works inside. Could someone please take a look? I have attached a screenshot with it giving white outside of the box upon hover. Thanks!</p>
  
  <p><a href="https://i.stack.imgur.com/DWD5D.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DWD5D.jpg" alt="enter image description here"></a></p>
  
  <pre><code>import java.awt.*;
  import java.awt.event.*;
  import java.awt.image.BufferedImage;
  import java.io.*;
  import java.net.*;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import javax.imageio.ImageIO;
  import javax.swing.*;
  import javax.swing.border.EmptyBorder;
  
  public class RGBValue {
  
  public static void main(String[] args) throws Exception {
  
       new RGBValue();
  }
  
  public RGBValue() {
      EventQueue.invokeLater(new Runnable() {
          @Override
          public void run() {
              try {
                  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
              } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
              }
  
              JFrame frame = new JFrame("Get the RGB");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.setLayout(new BorderLayout());
              try {
                  frame.add(new TestPane());
              } catch (Exception ex) {
                  Logger.getLogger(RGBValue.class.getName()).log(Level.SEVERE, null, ex);
              }
              frame.pack();
              frame.setLocationRelativeTo(null);
              frame.setVisible(true);                          
          }    
      });   
  }
  
  public class TestPane extends JPanel {
  
      private BufferedImage img;
      private JLabel label;
      private JPanel fields;
      private JTextField red;
      private JTextField green;
      private JTextField blue;
     // private JTextArea RGB;
  
      PointerInfo info = MouseInfo.getPointerInfo();
      Point point = info.getLocation();    
      Robot robot = new Robot();      
  
      public TestPane() throws Exception {
          setLayout(new GridBagLayout());
          GridBagConstraints gbc = new GridBagConstraints();
          gbc.gridwidth = GridBagConstraints.REMAINDER;
          label = new JLabel();
          try {
              URL url = new URL("https://c6.staticflickr.com/2/1520/24330829813_944c817720_b.jpg");
              img = ImageIO.read(url);  
              label.setIcon(new ImageIcon(img));
          } catch (IOException ex) {
              ex.printStackTrace();
          }
  
          add(label, gbc);
  
          fields = new JPanel();
          fields.setBorder(new EmptyBorder(5, 5, 5, 5));
          red = new JTextField(3);
          green = new JTextField(3);
          blue = new JTextField(3);
          fields.add(red);
          fields.add(green);
          fields.add(blue);
          add(fields, gbc);
  
          robot.delay(2000);
  
          label.addMouseMotionListener(new MouseAdapter() {
            //  @Override
              public void mouseMoved(MouseEvent e) {
  
           // while (true) {
                  //int packedInt = img.getRGB(e.getX(), e.getY());
                 // Color color = new Color(packedInt, true);
                 point = MouseInfo.getPointerInfo().getLocation();   
                 Color color = robot.getPixelColor((int)point.getX(), (int)point.getY());
                 // RGB.setText(color.getBlue() + color.getRed() + color.getGreen());
                 red.setText(Integer.toString(color.getRed()));
                 green.setText(Integer.toString(color.getGreen()));
                 blue.setText(Integer.toString(color.getBlue()));
                 fields.setBackground(color);
              } 
          });
  
      }
  
  }
  
  }
  </code></pre>