Reputation: 643
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);
}
Upvotes: 1
Views: 102
Reputation: 46239
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));
}
...
}
Upvotes: 1