2012-04-25 50 views
1

所以我剛開始使用小程序,並且當小程序加載時,我正在打開一扇門,並打開兩扇窗戶。當你點擊這些窗戶或門時,他們會關閉。我的問題是,我需要做些什麼來重新打開這些窗戶門。我想我需要以某種方式將布爾變量設置爲False並重繪。我會在哪裏做到這一點。我不需要你們爲我寫代碼,我只想知道我該怎麼做。Java House小程序

由於時間提前,

裏克

import java.awt.*; 
    import java.awt.event.*; 
    import java.applet.*; 

/** 
Rick Armstrong 
Chapter 14 - House Applet 
*/ 

    public class HouseApplet extends Applet {  
    boolean leftWin, rightWin, door; 

    public void init()  
    {  
    leftWin = false; 
    rightWin = false; 
    door = false;  

    setBackground(Color.white);  
    addMouseListener(new MyMouseListener());  
    } 

    public void paint(Graphics g) 
    { 
    super.paint(g); 

    // Draw the house. 
    g.setColor(Color.black); 
    g.drawRect(100, 100, 200, 100); 

    // Draw the roof 
    g.drawLine(80, 100, 320, 100); 
    g.drawLine(80, 100, 200, 40); 
    g.drawLine(200, 40, 320, 100); 

    // Draw the left window open. 
    g.fillRect(120, 130, 40, 40); 

    // Draw the right window open. 
    g.fillRect(240, 130, 40, 40); 

    // Draw the door open. 
    g.fillRect(180, 130, 40, 70);   

    if (leftWin) { 
     // Draw the left window closed. 
     g.setColor(Color.white); 
     g.fillRect(120, 130, 40, 40); 
     g.setColor(Color.black); 
     g.drawRect(120, 130, 40, 40); 
     g.drawLine(140, 130, 140, 170); 
     g.drawLine(120, 150, 160, 150); 

    } 

    if (rightWin) { 
     // Draw the right window closed. 
     g.setColor(Color.white); 
     g.fillRect(240, 130, 40, 40); 
     g.setColor(Color.black); 
     g.drawRect(240, 130, 40, 40); 
     g.drawLine(260, 130, 260, 170); 
     g.drawLine(240, 150, 280, 150); 

    } 

    if (door) { 
     // Draw the door closed. 
     g.setColor(Color.white); 
     g.fillRect(180, 130, 40, 70); 
     g.setColor(Color.black); 
     g.drawRect(180, 130, 40, 70); 
     g.fillOval(210, 165, 07, 07); 

    }  
    } 

    private class MyMouseListener implements MouseListener 
    { 
    public void mousePressed(MouseEvent e) 
    { 
    } 

    public void mouseClicked(MouseEvent e) 
    { 
     int currentx = e.getX(); 
     int currenty = e.getY(); 

     boolean WindowLeft = (currentx >= 120 && currentx < 160 && currenty >= 130 && currenty <= 170); 
     if (WindowLeft) 
     { 
      leftWin = true; 
      repaint();    
     } 

     boolean WindowRight = (currentx >= 240 && currentx < 280 && currenty >= 130 && currenty <= 170); 
     if (WindowRight) 
     { 
      rightWin = true; 
      repaint(); 
     } 

     boolean Door = (currentx >= 180 && currentx < 220 && currenty >= 40 && currenty <= 200); 
     if (Door) 
     {    
      door = true; 
      repaint();  
     } 

     else; 
    } 

    public void mouseReleased(MouseEvent e) 
    { 
    } 

    public void mouseEntered(MouseEvent e) 
    { 
    } 

    public void mouseExited(MouseEvent e) 
    { 
    } 
    } 

}

回答

3

也許我誤解的問題,但爲什麼你總是在事件處理程序的值設置爲true?

如果你想切換行爲,你可以簡單地寫:value = !value然後重畫。 由於您最初的值設置爲false,接下來點擊將其設置爲true,接下來爲false,等等,等等

例如:

if (WindowLeft) 
     { 
      leftWin = !leftWin; 
      repaint();    
     } 

注意,有可能爲你通過點擊比框架有更新視圖更快的速度引起「競態條件」,但這通常不是初始問題的問題。

順便說一句:在可讀性方面,考慮以一種傳達其含義的方式命名變量。例如,命名門喚起了布爾?不是真的。但openOpen是,它有助於解釋變量的意義及其轉換。

+0

很好的問題。當我寫它哈哈,它聽起來很好。我會試試Uri。感謝您的回覆。 – HKImpact 2012-04-25 00:29:10

+0

你有幾件事我可以做不同的Uri,我不否認那個哈哈。但是當你告訴我value =!value;那麼這對我來說都是有意義的。發佈後我覺得很蠢。我知道這很容易。非常感謝你! – HKImpact 2012-04-25 00:32:21

0
if (WindowRight) 
{ 
    rightWin = false; 
    repaint(); 
} 
else { 
    rightWin = true; 
    repaint(); 
} 

嘗試以上操作。當你點擊窗口內部時,它會關閉,否則會打開。