Java House 小程序

标签 java

所以我刚开始使用 Applets,当 applet 加载时,我正在制作一扇打开的门和 2 个打开的 window 的房子。当您单击这些 window 或门时,它们将关闭。不过,我的问题是我需要做什么才能重新打开这些 window 门。我想我需要以某种方式将 boolean 变量设置为 False 并重新绘制。我会在哪里做这个。我不需要你们帮我写代码,我只是想知道我应该做什么。

提前致谢

瑞克

   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)
     {
     }
  }

最佳答案

也许我误解了这个问题,但为什么您总是在事件处理程序中将值设置为 true?

如果你想要一个切换行为,你可以简单地写:value = !value 然后重新绘制。 由于您最初将值设置为 false,下一次点击会将其设置为 true,下一次点击将设置为 false,等等。

例如:

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

请注意,您可能会通过点击速度快于框架有机会更新 View 而导致某种“竞争条件”,但这通常不是初始问题的问题。

顺便说一句:就可读性而言,请考虑以传达变量含义的方式命名变量。例如,命名门是否会让人联想到 boolean 值?并不真地。但 doorOpen 是,它有助于解释变量的含义及其转换。

关于Java House 小程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10307797/

相关文章:

java - 锁定条件和特定值

Java 为什么需要实现接口(interface)?

java - @PathVariable 与 @RequestBody (哪一个以及何时?)

java - CPU 使用率在 grizzly http (REST) 服务器上经常达到 100% - JAVA8

java - 国家州城市密码数据

Java套接字编程头

java - 在maven多模块项目中构建单个模块

java - Java中线程的状态

java - 将巨大的包添加到新项目中(Eclipse)

java - 如何在没有 IntelliJ 的情况下在本地主机上运行 Spring 应用程序?