2012-07-15 185 views
1

请原谅我,如果对此的回答相当微不足道,因为我还没有编程一段时间。我的应用程序的目标是从我的框架中显示的图像中获取RGB值,其中(x,y)坐标由鼠标监听器给出,但是当我处于我的事件处理函数中时,我只能访问x,y值而不是我的BufferedImage。帮帮我!我一直坚持了几个小时!从MouseHandler类我需要数据进出我的mouselistener事件处理程序

代码:

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

是这样你所有的代码?你不使用ActionListeners吗? – skytreader 2012-07-15 14:32:04

回答

1

我建议当您创建MouseHandler类一起发送您的BufferedImage

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

这样做!谢谢一堆! – 2012-07-15 14:50:01

相关问题