I need data either in or out of my mouselistener event handler

Forgive me if the answer to this is fairly trivial as I haven't been programming for a while. My application's goal is to grab the RGB values from the image I have displayed in my frame, where the (x,y) coordinate is given by a mouse listener, however when I'm in my event handler, I only have access to the x,y values and not my BufferedImage. Help! I've been stuck for hours!!

code from MouseHandler class:

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

Code from application class:

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

Answers


I would suggest sending your BufferedImage along when you create your MouseHandler class:

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));
  }
  ...
}

Need Your Help

UIScrollView with over 100 images

iphone image uiscrollview paging

I have implemented a UIScrollView on the iPhone that contains 100 image slots which I fill only the currently visible page, the one before it, and the one after it as the user scrolls.

PNG image is not loading properly in OpenCV

c++ image opencv image-processing

I am using the below code to view the following image.