2012-04-25 68 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(); 
} 

尝试以上操作。当你点击窗口内部时,它会关闭,否则会打开。