当鼠标悬停指向照片的一个位置时,如何识别颜色?
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);
java.lang.ArrayIndexOutOfBoundsException出错:协调越界!
答案 0 :(得分:7)
问题是鼠标的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);
不要在语法上引用我,但这是基本的想法。