6

By knowing the coordinates of a point in a JPanel, how can I get its color?

moorara
  • 3,897
  • 10
  • 47
  • 60
  • 1) What feature are you trying to offer the user? Explain it to me as if I were a potential user trying to decide whether to buy (or download) the software. 2) [What have you tried?](http://www.whathaveyoutried.com/) – Andrew Thompson Nov 09 '12 at 12:49
  • 1
    Could this please be moved back to open. I came across this very issue in something I was working on, and was hoping others may have come across something similar as well and could possibly asnwer the question. So far, Dan's answer seems to be the only route I can think of. Thank you. – Boltimuss Jan 13 '14 at 23:17
  • Really don't understand why this is closed... – jjazzboss Dec 17 '22 at 10:23

1 Answers1

5

Draw the content of the panel inside a Graphics2D object created from a BufferedImage and then retrieve the pixel color:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g2 = image.createGraphics();
_mainPanel.paint(g2);
image.getColorModel().getRGB(pixel);
g2.dispose();
Dan D.
  • 32,246
  • 5
  • 63
  • 79