1

如果这个问题的答案相当琐碎,请原谅我,因为我已经有一段时间没有编程了。我的应用程序的目标是从我在我的框架中显示的图像中获取 RGB 值,其中 (x,y) 坐标由鼠标侦听器给出,但是当我在我的事件处理程序中时,我只能访问x,y 值而不是我的 BufferedImage。帮助!我已经被困了好几个小时了!!

MouseHandler 类的代码:

    public void mouseClicked (MouseEvent e)
{
    int x = e.getX();
    int y = e.getY();
    System.out.printf("You clicked at: %d,%d\n", x, y);
}

应用程序类的代码:

    public static void main(String args[]) 
{
    String file_name = args[0];

    BufferedImage image = readImage2(file_name);
    Frame frame = createFrame(file_name);

    //somehow get x,y from listener;
    //int RGB = image.getRGB(x,y);
}
4

1 回答 1

1

我建议您BufferedImage在创建课程时发送您的MouseHandler

public class MouseHandler implents MouseListener {

  private BufferedImage image;

  public MouseHandler(BufferedImage image) {
    this.image = image;
  }
  public void mouseClicked (MouseEvent e) {
    int x = e.getX();
    int y = e.getY();
    System.out.printf("You clicked at: %d,%d\n", x, y);
    System.out.printf("Color is: %d", image.getRGB(x, y));
  }
  ...
}
于 2012-07-15T14:37:41.870 回答