2012-07-15 76 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

相關問題