当鼠标悬停指向照片的一个帖子时,如何识别颜色?
BufferedImage image = new BufferedImage("blueball.jpg");
Project() {
jFrame.setSize(new Dimension(500, 320));
jFrame.getContentPane().setLayout(null);
colorLabelText.setBounds(new Rectangle(310, 210, 50, 30));
colorLabelText.setText("Color :");
colorLabel.setBounds(new Rectangle(370, 210, 100, 30));
photoLabel.setBounds(new Rectangle(20, 20, 220, 250));
photoLabel.addMouseListener(new RecognizeColorActionListener());
jFrame.getContentPane().add(photoLabel);
jFrame.getContentPane().add(colorLabelText);
jFrame.getContentPane().add(colorLabel);
jFrame.setVisible(true);
}
class RecognizeColorActionListener implements MouseListener {
@Override
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
int imgx = image.getMinX();
int imgy = image.getMinY();
int c = image.getRGB(x - imgx, y - imgy);
Run Code Online (Sandbox Code Playgroud)
java.lang.ArrayIndexOutOfBoundsException出错:坐标超出界限!
问题是鼠标的X和Y坐标不对应于图像的X和Y坐标.将其更改为以下内容:
int x = e.getX();
int y = e.getY();
int imgx = image.getX();
int imgy = image.getY();
int c = image.getRGB(x - imgx, y - imgy);
Run Code Online (Sandbox Code Playgroud)
不要在语法上引用我,但这是基本的想法.