How to get the color of a point in a JPanel? [clos

2019-01-19 06:15发布

问题:

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

回答1:

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