2011-08-28 17 views
1

我正在編寫一個程序,該程序具有正方形網格,當您單擊它時,它會將顏色從黑色更改爲紅色。我正在寫第一個方塊的if語句,並且不讓我用圖形做任何事情。我如何1)改變顏色爲紅色,2)在同一個地方畫一個新的方塊,3)爲什麼不像MouseHandler類聲明那樣?無法將圖形應用於if語句

import java.awt.*; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
public class toggle extends JPanel{ 
    public void paintComponent(Graphics g){ 
     super.paintComponents(g); 
     g.fillRect(0,0,20,20); 
     g.fillRect(21,0,20,20); 
     g.fillRect(42,0,20,20); 
     g.fillRect(63,0,20,20); 
     g.fillRect(84,0,20,20); 
     g.fillRect(105,0,20,20); 
     g.fillRect(126,0,20,20); 
     g.fillRect(147,0,20,20); 
     g.fillRect(168,0,20,20); 
     g.fillRect(189,0,20,20); 
     g.fillRect(190,0,20,20); 
     // 
     g.fillRect(0,21,20,20); 
     g.fillRect(21,21,20,20); 
     g.fillRect(42,21,20,20); 
     g.fillRect(63,21,20,20); 
     g.fillRect(84,21,20,20); 
     g.fillRect(105,21,20,20); 
     g.fillRect(126,21,20,20); 
     g.fillRect(147,21,20,20); 
     g.fillRect(168,21,20,20); 
     g.fillRect(189,21,20,20); 
     g.fillRect(190,21,20,20); 
     // 
     g.fillRect(0,42,20,20); 
     g.fillRect(21,42,20,20); 
     g.fillRect(42,42,20,20); 
     g.fillRect(63,42,20,20); 
     g.fillRect(84,42,20,20); 
     g.fillRect(105,42,20,20); 
     g.fillRect(126,42,20,20); 
     g.fillRect(147,42,20,20); 
     g.fillRect(168,42,20,20); 
     g.fillRect(189,42,20,20); 
     g.fillRect(190,42,20,20); 
     // 
     g.fillRect(0,63,20,20); 
     g.fillRect(21,63,20,20); 
     g.fillRect(42,63,20,20); 
     g.fillRect(63,63,20,20); 
     g.fillRect(84,63,20,20); 
     g.fillRect(105,63,20,20); 
     g.fillRect(126,63,20,20); 
     g.fillRect(147,63,20,20); 
     g.fillRect(168,63,20,20); 
     g.fillRect(189,63,20,20); 
     g.fillRect(190,63,20,20); 
     // 
     g.fillRect(0,84,20,20); 
     g.fillRect(21,84,20,20); 
     g.fillRect(42,84,20,20); 
     g.fillRect(63,84,20,20); 
     g.fillRect(84,84,20,20); 
     g.fillRect(105,84,20,20); 
     g.fillRect(126,84,20,20); 
     g.fillRect(147,84,20,20); 
     g.fillRect(168,84,20,20); 
     g.fillRect(189,84,20,20); 
     g.fillRect(190,84,20,20); 
     // 
     g.fillRect(0,105,20,20); 
     g.fillRect(21,105,20,20); 
     g.fillRect(42,105,20,20); 
     g.fillRect(63,105,20,20); 
     g.fillRect(84,105,20,20); 
     g.fillRect(105,105,20,20); 
     g.fillRect(126,105,20,20); 
     g.fillRect(147,105,20,20); 
     g.fillRect(168,105,20,20); 
     g.fillRect(189,105,20,20); 
     g.fillRect(190,105,20,20); 
     // 
     g.fillRect(0,126,20,20); 
     g.fillRect(21,126,20,20); 
     g.fillRect(42,126,20,20); 
     g.fillRect(63,126,20,20); 
     g.fillRect(84,126,20,20); 
     g.fillRect(105,126,20,20); 
     g.fillRect(126,126,20,20); 
     g.fillRect(147,126,20,20); 
     g.fillRect(168,126,20,20); 
     g.fillRect(189,126,20,20); 
     g.fillRect(190,126,20,20); 
     // 
     g.fillRect(0,147,20,20); 
     g.fillRect(21,147,20,20); 
     g.fillRect(42,147,20,20); 
     g.fillRect(63,147,20,20); 
     g.fillRect(84,147,20,20); 
     g.fillRect(105,147,20,20); 
     g.fillRect(126,147,20,20); 
     g.fillRect(147,147,20,20); 
     g.fillRect(168,147,20,20); 
     g.fillRect(189,147,20,20); 
     g.fillRect(190,147,20,20); 
     // 
     g.fillRect(0,168,20,20); 
     g.fillRect(21,168,20,20); 
     g.fillRect(42,168,20,20); 
     g.fillRect(63,168,20,20); 
     g.fillRect(84,168,20,20); 
     g.fillRect(105,168,20,20); 
     g.fillRect(126,168,20,20); 
     g.fillRect(147,168,20,20); 
     g.fillRect(168,168,20,20); 
     g.fillRect(189,168,20,20); 
     g.fillRect(190,168,20,20); 
     // 
     g.fillRect(0,189,20,20); 
     g.fillRect(21,189,20,20); 
     g.fillRect(42,189,20,20); 
     g.fillRect(63,189,20,20); 
     g.fillRect(84,189,20,20); 
     g.fillRect(105,189,20,20); 
     g.fillRect(126,189,20,20); 
     g.fillRect(147,189,20,20); 
     g.fillRect(168,189,20,20); 
     g.fillRect(189,189,20,20); 
     g.fillRect(190,189,20,20); 
    } 

    public toggle(){ 
     super(); 
     JFrame frame=new JFrame(); 
     frame.setTitle("toggle"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(this); 
     frame.setSize(226, 247); 
     frame.setVisible(true); 

     MouseHandler hand=new MouseHandler(); 
     addMouseListener(hand); 

    } 

    public class MouseHandler implements MouseListener{ 
     public void MousePressed (MouseEvent e){ 

      if(e.getX()<20&&e.getX()>0&&e.getY()<20&&e.getY()>0){ 


      } 
     } 
    } 

    public static void main (String[]args){ 
     new toggle(); 
    } 


} 

回答

1

1和2)你需要保持狀態每平方米,在boolean[][]領域,例如,如果您的網格每平方米被點擊或沒有哪個會告訴。在你的paintComponent方法中,檢查每個方塊的狀態,並調用g.setColor,相應地將Color.BLACKColor.RED作爲參數。

3)您需要實現的MouseListener接口的所有方法:

public class MouseHandler implements MouseListener { 
    public void mousePressed(MouseEvent e) { 
     // your logic here 
    } 

    public void mouseReleased(MouseEvent e) { 
     // leave method empty, since you don't need to listen to this event 
    } 

    // Same as above for mouseClicked, mouseEntered, mouseExited 
} 

或延長MouseAdapter類並覆蓋mousePressed,如果你不爲其他MouseListener事件不在意:

public class MouseHandler extends MouseAdapter { 
    public void mousePressed(MouseEvent e) { 
     // your logic here 
    } 

    // No need to override the other methods, since 
    // MouseAdapter already has empty implementations for them 
} 

此外,您正在執行的方法的正確名稱是mousePressed(請注意小寫字母m)。

+0

非常感謝,這確實有幫助,但我仍然沒有得到如何解決MouseListener問題。 – tussya

+0

我編輯了我的答案,以便更清楚地包含這兩個選項。 – lhballoti

+0

好吧,偉大的eveything工程,除了現在認爲布爾型的「bos」總是如此,即使我沒有點擊它。我正在爲每個廣場寫if語句。這是針對第一個:if(e.getX()<20 && e.getX()> 0 && e.getY()<20 && e.getY()> 0){0} {bbb = true; }我應該改變它的工作方式? – tussya

1

3)爲什麼不能像MouseHandler類聲明那樣?

當您實現一個接口時,您必須爲所有方法提供一個實現或聲明實現abstract。在這種情況下,您尚未實現單一方法 - Java區分大小寫,MousePressed與您想要實現的方法mousePressed不同。

此外,你會發現有幾個接口比你感興趣的方法有更多的方法,你最終會提供空方法來滿足編譯器。在這種情況下,您可能會看看是否有可以擴展的「適配器」類。在這種情況下,你會發現MouseAdapter有用。

+0

非常感謝,所有的答案都有所幫助。 – tussya

+0

「MouseAdapter」的+1。 – trashgod

1

當然你不需要全部g.fillRect(0,0,20,20);您可以使用循環來處理這個問題,因爲每行中的第一個參數都有一個發生模式。