-1

I am writing a MouseListener click event, when I click on a JLabel it will give me the RGB value of the pixel that I clicked.

I can use

mybufferedImage.getRGB(e.getPoint().x, e.getPoint().y

However, I am having trouble retrieving the Icon as a BufferedImage.

I KNOW that I can retrieve the Icon and then convert to BufferedImage, but I am wondering if there is an equivalent way of getting the RGB value of the Icon without having to convert to BufferedImage? My main concern is performance, every time I retrieve the RGB value I don't want to have to convert the entire image, especially when I am working with very large images.

Thank you

ThePrince
  • 818
  • 2
  • 12
  • 26
  • 1
    Personally, I'd not use a `JLabel` for this and instead use a `BufferedImage` and a custom component to paint it, you'd gain more control and flexibility – MadProgrammer May 21 '21 at 02:35
  • 1
    You could also paint the icon to `BufferedImage`, but you might not get the same color model :/ – MadProgrammer May 21 '21 at 02:35
  • 1
    Maybe consider something more [like this example](https://stackoverflow.com/questions/18158550/zoom-box-for-area-around-mouse-location-on-screen/18158845#18158845) or [this example](https://stackoverflow.com/questions/16139030/java-rgb-value-code-robot-class/16139916#16139916) – MadProgrammer May 21 '21 at 02:43

1 Answers1

2

You can use the Robot.getPixelColor(...) method to get the pixel color at a given point on the screen.

You will first need to convert the mouse point to a point on the screen using the SwingUtilities.convertPointToScreen(...) method.

My main concern is performance,

Not really an issue. I mean how fast can you click the mouse?

camickr
  • 321,443
  • 19
  • 166
  • 288