2012-08-04 28 views
0

我一直在搜索谷歌過去一天半試圖弄清楚這一點。 正如您所看到的,我正在嘗試使mouseClicked更新變量clickcolumnindex和clickrowindex,以便使mouseClicked方法更新中的replaint()方法更新,從而在另一列/行中顯示通過顏色顯示創建的計數器。變量不會傳遞到方法油漆

我覺得這可能是一個非常簡單的修復方法,在我嘗試加載的東西之後,我有點失落了我的思路。

幫助將不勝感激。

import java.awt.*; 
import java.awt.geom.*; 
import javax.swing.*; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseEvent; 

      class GridPanel extends JPanel implements MouseListener 
      { 
       int numCols; 
       int numRows; 
       int w; 
       int h; 
       int x; 
       int y; 
       int clickcolumnindex; 
       int clickrowindex; 


       public void mouseReleased(MouseEvent event) 
       { 
        System.out.println("Mouse released."); 
       } 
       public void mousePressed(MouseEvent event) 
       { 
        System.out.println("Mouse Pressed"); 
       } 
       public void mouseClicked(MouseEvent event) 
       { 
        int clickcolumnindex = 7 * x/getWidth(); 
        int clickrowindex = 5 * y/getHeight(); 
        int x = event.getX(); 
        int y = event.getY(); 
        System.out.println("x position of click is: " +x); 
        System.out.println("y position of click is: " +y); 
        System.out.println("Click is in column " +clickcolumnindex); 
        System.out.println("Click is in row " +clickrowindex); 
        repaint(); 

       } 


    public void mouseReleased(MouseEvent event) { 
     System.out.println("Mouse released."); 
    } 

    public void mousePressed(MouseEvent event) { 
     System.out.println("Mouse Pressed"); 
    } 

    public void mouseClicked(MouseEvent event) { 
     int x = event.getX(); 
     int y = event.getY(); 
     System.out.println("x position of click is: " + x); 
     System.out.println("y position of click is: " + y); 
     System.out.println("Click is in column " + clickcolumnindex); 
     System.out.println("Click is in row " + clickrowindex); 
     repaint(); 
    } 

    public void mouseEntered(MouseEvent event) { 
     System.out.println("Mouse entered."); 
    } 

    public void mouseExited(MouseEvent event) { 
     System.out.println("Mouse exited."); 
    } 

    public GridPanel(int nc, int nr) { 
     numCols = nc; 
     numRows = nr; 
     addMouseListener(this); 
    } 

    Rectangle getRect(int thisCol, int thisRow) { 
     // if input is out of range, return "null" 
     if (thisCol < 0 || thisRow < 0) 
     return null; 
     if (thisCol >= numCols || thisRow >= numRows) 
     return null; 
     // otherwise, make and return the Rectangle 
     int w = getWidth()/numCols; 
     int h = getHeight()/numRows; 
     int x = thisCol * w; 
     int y = thisRow * h; 
     Rectangle myRect = new Rectangle(x, y, w, h); 
     return myRect; 
    } 

    public void paint(Graphics g) { 
     g.setColor(Color.gray); 
     g.fillRect(0, 0, getWidth(), getHeight()); 
     g.setColor(Color.black); 
     Graphics2D g2 = (Graphics2D) g; 
     // we'll use Graphics2D for it's "draw" method - 
     // neater than the Graphics "drawRect" suppled 
     // (which you could also use) 
     for (int i = 0; i < numCols; i++) { 
     for (int j = 0; j < numRows; j++) { 
      Rectangle r = getRect(i, j); 
      g2.draw(r); 
     } 
     } 
     g2.setColor(Color.blue); 
     Rectangle r1 = getRect(clickcolumnindex, clickrowindex); 
     g2.fillOval(r1.x, r1.y, r1.width, r1.height); 
     System.out.println("variable updates " + clickcolumnindex + clickrowindex); 
    } 

    // copied from the W2MouseEvents for convenience 
    // (so we can run the program from GridPanel class too!) 
    public static void main(String[] args) { 
     W2MouseEvents w = new W2MouseEvents(); 
     w.setVisible(true); 
    } 
} 

W2MouseEvents.java:

import javax.swing.JFrame; 

public class W2MouseEvents extends JFrame { 
    GridPanel myGridPanel; 

    public static void main(String[] args) { 
     W2MouseEvents w = new W2MouseEvents(); 
     w.setVisible(true); 
    } 

    public W2MouseEvents() { 
     setTitle("Workshop 2 (Mouse Events): starting code"); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setSize(500, 400); 
     setLocation(300, 300); 
     myGridPanel = new GridPanel(7, 5); 
     add(myGridPanel); 
    } 

} 

回答

3

至少有兩個問題: 變量不會神奇地自動更新自己。 如果更改了x,則右側沒有得到x的變量(假設它們在相同範圍內)得到更新。 澄清:

int x = 4; 
int var = x + 1; // var == 5 
x = 5 // var still is 5, not 6 

假設你想改變xy「全球」(如「你的類的實例的屬性」),你就必須解決第二個問題是

public void mouseClicked(MouseEvent event) 
{ 
    int x = event.getX(); 
    int y = event.getY(); 
    // ... 
} 

這裏會發生什麼事是你聲明瞭兩個新的局部變量xy分配任何值給他們,你的「全局」變量xy不會改變 ,除非您省略聲明int。 這是不是一個問題,如果你重新計算值的clickColumnIndexclickRowIndex內mouseClicked/mousePressed/...方法之一。

編輯 我看到你移動的clickColumnIndex計算和它的兄弟到mouseClicked方法。但如上所述,您正在創建兩個變量,其中隱藏「全局」變量。只需刪除int

+0

好皮卡! 1 +投票 – 2012-08-04 15:57:02

+0

我想你有什麼建議,但仍然沒有得到任何地方。這裏是我從我的println中獲得的應該在paint中更新的變量的代碼。變量更新00 變量更新00 鼠標進入。 鼠標按下 鼠標釋放。點擊的 x位置是:136點擊的 y位置是:88 點擊是在列0 點擊是行0 變量更新00 鼠標按下 鼠標釋放。點擊的 x位置是:136點擊的 y位置是:88 點擊是在列0 點擊是行0 變量更新00 鼠標按下 鼠標釋放。 – 2012-08-04 16:04:31

+0

@LewisSherlock你究竟試過了什麼? – Tedil 2012-08-04 16:06:27

1

您設置clickcolumnindex你把它聲明,鼠標事件不斷髮生之前。您需要將其設置在鼠標偵聽器中。

換句話說,這是沒有意義的:

 class GridPanel extends JPanel implements MouseListener 
     { 
      int numCols; 
      int numRows; 
      int w; 
      int h; 
      int x; 
      int y; 
      int clickcolumnindex = 7 * x/getWidth(); 
      int clickrowindex = 5 * y/getHeight(); 

,因爲此時x爲0,所以是的getWidth()(驚訝你沒有得到通過這裏的零錯誤劃分)。

您想要在MouseListener的mousePressed(...)方法中設置clickXXXindex。我更喜歡使用mousePressed(...)而不是mouseClicked(...),因爲如果您在按下鼠標後移動鼠標,前者不會太挑剔。

此外,您還需要在JPanel的paintComponent(...)方法中執行繪圖,而不是其paint(...)方法。

+0

H.F.O.E - 它已經在頁面頂部(包括計算)? – 2012-08-04 15:46:21

+0

@Lewis:在那個位置計算沒有意義。它只在創建類的對象時進行一次計算,而不再次執行。點擊鼠標時需要進行計算。 – 2012-08-04 15:47:50

+0

我現在已經完成了這個工作,但是paint方法裏面的變量仍然是零。 (感謝您的幫助!) – 2012-08-04 15:54:02

1

中的陳述:

int clickcolumnindex = 7 * x/getWidth(); 
    int clickrowindex = 5 * y/getHeight(); 

不允許構造函數,方法或靜態初始化塊之外。

正如HoverMeister所指出的那樣,這些只會被攻擊一次。爲什麼不有一個單獨的方法來更新這些變量和隨後的repaint()?