Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java小程序AWT-EventQueue-1异常_Java_Exception_Applet_Indexoutofboundsexception - Fatal编程技术网

Java小程序AWT-EventQueue-1异常

Java小程序AWT-EventQueue-1异常,java,exception,applet,indexoutofboundsexception,Java,Exception,Applet,Indexoutofboundsexception,我用java小程序编写了生活游戏的一个简单实现。 以下是和的源代码 当我单击按钮获取下一次迭代时,会抛出这些异常 Z:\GameOfLife>appletviewer driver.html Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException: 65 at GameOfLifeApplet.mouseClicked(GameOfLifeApplet.java:63)

我用java小程序编写了生活游戏的一个简单实现。 以下是和的源代码

当我单击按钮获取下一次迭代时,会抛出这些异常

Z:\GameOfLife>appletviewer driver.html
Exception in thread "AWT-EventQueue-1" java.lang.ArrayIndexOutOfBoundsException:
 65
        at GameOfLifeApplet.mouseClicked(GameOfLifeApplet.java:63)
        at java.awt.Component.processMouseEvent(Component.java:6219)
        at java.awt.Component.processEvent(Component.java:5981)
        at java.awt.Container.processEvent(Container.java:2041)
        at java.awt.Component.dispatchEventImpl(Component.java:4583)
        at java.awt.Container.dispatchEventImpl(Container.java:2099)
        at java.awt.Component.dispatchEvent(Component.java:4413)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThre    ad.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThre    ad.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

尝试此代码,将按钮添加一次,而不是每次调用
paint()
。请注意,如果您在网格外(而不是在按钮上)单击,此源仍会抛出AIOOBE,但这似乎是一个基本的逻辑错误,您应该在按钮修复后进行调查

// <applet code='GameOfLifeApplet' width=580 height=650></applet>
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class GameOfLifeApplet extends Applet implements MouseListener,ActionListener
{
  //the x and y coordinates to get the location of the clicked points
  private int xCo, yCo;
  private int diff_x, diff_y;
  private GameOfLife game = new GameOfLife();
  private Button nextButton = null;
  public void init()
  {
    setLayout(null);
    nextButton = new Button("Next Stage");
    diff_x = diff_y = 600 / game.getGridSize();
    nextButton.setLocation(250, 575);
    nextButton.setSize(120, 30);
    // add the button once only!
    add(nextButton);
    addMouseListener(this);
  }

  private void drawEmptyGrid(Graphics g)
  {
    g.setColor(Color.white);
    g.fillRect(0,0,600,600);
    g.setColor(Color.black);
    for(int i=0;i<game.getGridSize();++i)
    {
      g.drawLine(0,i*diff_x,600,i*diff_x);
      g.drawLine(i*diff_x,0,i*diff_x,600);
    }
    g.setColor(Color.white);
  }

  public void paint(Graphics g)
  {
    drawEmptyGrid(g);
    g.setColor(Color.red);
    for(int i=0;i<game.getGridSize();++i)
    {
      for(int j=0;j<game.getGridSize();++j)
      {
        if( game.grid[i][j] )
        {
          g.fillRect(i*diff_x,j*diff_y,diff_x,diff_y);
        }
      }
    }
    g.setColor(Color.white);
  }

 // This method will be called when the mouse has been clicked.
 public void mouseClicked (MouseEvent me) {

  // Save the coordinates of the click lke this.
  xCo = me.getX();
  yCo = me.getY();

  int x_init = xCo / diff_x;
  int y_init = yCo / diff_y;

  System.out.println(x_init + "x" + y_init);
  game.grid[x_init][y_init] = true;
  //show the results of the click
  repaint();

 }

 // This is called when the mous has been pressed
 public void mousePressed (MouseEvent me) {}

 // When it has been released
 // not that a click also calls these Mouse-Pressed and Released.
 // since they are empty nothing hapens here.
 public void mouseReleased (MouseEvent me) {}

 // This is executed when the mouse enters the applet. it will only
 // be executed again when the mouse has left and then re-entered.
 public void mouseEntered (MouseEvent me) {}

 // When the Mouse leaves the applet.
 public void mouseExited (MouseEvent me) {}

 public void actionPerformed(ActionEvent evt)
  {
  // Here we will ask what component called this method
    if (evt.getSource() == nextButton)
    {
      System.out.println("I got clicked!");
      game.nextIteration();
      repaint();
    }
  }

}

class GameOfLife
{
  private final int GRID_SIZE = 64;
  public boolean [][] grid = new boolean[GRID_SIZE][GRID_SIZE];
  //default constructor
  public GameOfLife()
  {
    for(int i=0;i<GRID_SIZE;++i)
    {
      for(int j=0;j<GRID_SIZE;++j)
    {
      grid[i][j] = false;
    }
    }
  }

  public int getGridSize()
  {
    return GRID_SIZE;
  }

  public int getLiveNeighbors(int i,int j)
  {
    int neighbors = 0;
    for( int tmp_i = i-1; tmp_i <= i+1; ++tmp_i )
    {
      for( int tmp_j = j-1; tmp_j <= j+1; ++tmp_j )
      {
        if( tmp_i < 0 || tmp_i >= GRID_SIZE || tmp_j < 0 || tmp_j >= GRID_SIZE )
        {}
        else
        {
          if( grid[tmp_i][tmp_j] )
          {
            neighbors++;
          }
        }
      }
    }
    return neighbors;
  }

  public void nextIteration()
  {
    boolean [][] newGrid = new boolean[GRID_SIZE][GRID_SIZE];

    for(int i=0;i<GRID_SIZE;++i)
    {
      for(int j=0;j<GRID_SIZE;++j)
      {
        newGrid[i][j] = grid[i][j];
      }
    }

    for( int i=0;i<GRID_SIZE;++i)
    {
      for( int j=0;j<GRID_SIZE;++j)
      {
        int my_neighbors = getLiveNeighbors(i,j);
        if( !newGrid[i][j] && my_neighbors == 3)
        {
          grid[i][j] = true;
        }

        else if( newGrid[i][j] && ( my_neighbors == 2 || my_neighbors == 3 ) )
        {
          grid[i][j] = true;
        }

        else
        {
          grid[i][j] = false;
        }
      }
    }
    System.out.println("Change of assignment");
  }
}
其他事项
  • 代码将自定义绘制移动到
    面板
    。这解决了直接绘制顶级容器的常见问题。它还允许在不同的容器中轻松重复使用相同的GUI。在这种情况下,小程序和“应用程序”(通常放在一个框架中)都有一个
    JOptionPane
    。它现在被称为“混合小程序/应用程序”(更易于测试)
  • 自定义绘制的组件<代码>生态系统(耸耸肩)告知布局它希望的大小。这有助于我们避免设置任何内容的大小或边界
  • 按钮的大小将与需要的大小完全相同

  • 尝试此代码,将按钮添加一次,而不是每次调用
    paint()
    。请注意,如果您在网格外(而不是在按钮上)单击,此源仍会抛出AIOOBE,但这似乎是一个基本的逻辑错误,您应该在按钮修复后进行调查

    // <applet code='GameOfLifeApplet' width=580 height=650></applet>
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    
    public class GameOfLifeApplet extends Applet implements MouseListener,ActionListener
    {
      //the x and y coordinates to get the location of the clicked points
      private int xCo, yCo;
      private int diff_x, diff_y;
      private GameOfLife game = new GameOfLife();
      private Button nextButton = null;
      public void init()
      {
        setLayout(null);
        nextButton = new Button("Next Stage");
        diff_x = diff_y = 600 / game.getGridSize();
        nextButton.setLocation(250, 575);
        nextButton.setSize(120, 30);
        // add the button once only!
        add(nextButton);
        addMouseListener(this);
      }
    
      private void drawEmptyGrid(Graphics g)
      {
        g.setColor(Color.white);
        g.fillRect(0,0,600,600);
        g.setColor(Color.black);
        for(int i=0;i<game.getGridSize();++i)
        {
          g.drawLine(0,i*diff_x,600,i*diff_x);
          g.drawLine(i*diff_x,0,i*diff_x,600);
        }
        g.setColor(Color.white);
      }
    
      public void paint(Graphics g)
      {
        drawEmptyGrid(g);
        g.setColor(Color.red);
        for(int i=0;i<game.getGridSize();++i)
        {
          for(int j=0;j<game.getGridSize();++j)
          {
            if( game.grid[i][j] )
            {
              g.fillRect(i*diff_x,j*diff_y,diff_x,diff_y);
            }
          }
        }
        g.setColor(Color.white);
      }
    
     // This method will be called when the mouse has been clicked.
     public void mouseClicked (MouseEvent me) {
    
      // Save the coordinates of the click lke this.
      xCo = me.getX();
      yCo = me.getY();
    
      int x_init = xCo / diff_x;
      int y_init = yCo / diff_y;
    
      System.out.println(x_init + "x" + y_init);
      game.grid[x_init][y_init] = true;
      //show the results of the click
      repaint();
    
     }
    
     // This is called when the mous has been pressed
     public void mousePressed (MouseEvent me) {}
    
     // When it has been released
     // not that a click also calls these Mouse-Pressed and Released.
     // since they are empty nothing hapens here.
     public void mouseReleased (MouseEvent me) {}
    
     // This is executed when the mouse enters the applet. it will only
     // be executed again when the mouse has left and then re-entered.
     public void mouseEntered (MouseEvent me) {}
    
     // When the Mouse leaves the applet.
     public void mouseExited (MouseEvent me) {}
    
     public void actionPerformed(ActionEvent evt)
      {
      // Here we will ask what component called this method
        if (evt.getSource() == nextButton)
        {
          System.out.println("I got clicked!");
          game.nextIteration();
          repaint();
        }
      }
    
    }
    
    class GameOfLife
    {
      private final int GRID_SIZE = 64;
      public boolean [][] grid = new boolean[GRID_SIZE][GRID_SIZE];
      //default constructor
      public GameOfLife()
      {
        for(int i=0;i<GRID_SIZE;++i)
        {
          for(int j=0;j<GRID_SIZE;++j)
        {
          grid[i][j] = false;
        }
        }
      }
    
      public int getGridSize()
      {
        return GRID_SIZE;
      }
    
      public int getLiveNeighbors(int i,int j)
      {
        int neighbors = 0;
        for( int tmp_i = i-1; tmp_i <= i+1; ++tmp_i )
        {
          for( int tmp_j = j-1; tmp_j <= j+1; ++tmp_j )
          {
            if( tmp_i < 0 || tmp_i >= GRID_SIZE || tmp_j < 0 || tmp_j >= GRID_SIZE )
            {}
            else
            {
              if( grid[tmp_i][tmp_j] )
              {
                neighbors++;
              }
            }
          }
        }
        return neighbors;
      }
    
      public void nextIteration()
      {
        boolean [][] newGrid = new boolean[GRID_SIZE][GRID_SIZE];
    
        for(int i=0;i<GRID_SIZE;++i)
        {
          for(int j=0;j<GRID_SIZE;++j)
          {
            newGrid[i][j] = grid[i][j];
          }
        }
    
        for( int i=0;i<GRID_SIZE;++i)
        {
          for( int j=0;j<GRID_SIZE;++j)
          {
            int my_neighbors = getLiveNeighbors(i,j);
            if( !newGrid[i][j] && my_neighbors == 3)
            {
              grid[i][j] = true;
            }
    
            else if( newGrid[i][j] && ( my_neighbors == 2 || my_neighbors == 3 ) )
            {
              grid[i][j] = true;
            }
    
            else
            {
              grid[i][j] = false;
            }
          }
        }
        System.out.println("Change of assignment");
      }
    }
    
    其他事项
  • 代码将自定义绘制移动到
    面板
    。这解决了直接绘制顶级容器的常见问题。它还允许在不同的容器中轻松重复使用相同的GUI。在这种情况下,小程序和“应用程序”(通常放在一个框架中)都有一个
    JOptionPane
    。它现在被称为“混合小程序/应用程序”(更易于测试)
  • 自定义绘制的组件<代码>生态系统(耸耸肩)告知布局它希望的大小。这有助于我们避免设置任何内容的大小或边界
  • 按钮的大小将与需要的大小完全相同

  • 首先,我认为你读错了异常跟踪。异常为
    ArrayIndexOutOfBoundsException
    ,发生在
    GameOfLifeApplet.java
    的第63行。您的应用程序是一个小程序,或者在线程
    AWT-EventQueue-1
    上发生异常与此无关


    根本原因是您没有正确同步模型和视图关于网格中有多少单元格的想法。至少,你应该考虑在访问数组元素之前,用户确实在网格内部点击了。

    首先,我认为你正在读取异常跟踪错误。异常为
    ArrayIndexOutOfBoundsException
    ,发生在
    GameOfLifeApplet.java
    的第63行。您的应用程序是一个小程序,或者在线程
    AWT-EventQueue-1
    上发生异常与此无关


    根本原因是您没有正确同步模型和视图关于网格中有多少单元格的想法。至少,应该考虑用户在访问数组元素之前确实在网格内单击。< /P>我不知道如何将绘制区域添加到布局管理器中。如果我没有添加setLayout(null),那么按钮将自己定位在顶部。好的-我们在同一页上。对我来说,编写示例代码比解释更容易,所以请继续关注“Real Soon Now”的更新……感谢您的精彩回答,我从中学到了很多。你太棒了!我不知道如何将绘制区域添加到布局管理器。如果我没有添加setLayout(null),那么按钮将自己定位在顶部。好的-我们在同一页上。对我来说,编写示例代码比解释更容易,所以请继续关注“Real Soon Now”的更新……感谢您的精彩回答,我从中学到了很多。你太棒了!