JAVA:我正在尝试制作基于2D数组和图形的Connect4游戏



>后端程序使用扫描仪输入并仅打印出矩阵,但我在图形方面遇到了这个问题。当我只制作内部带有椭圆形的矩形时效果很好,但是当我在故事中引入 2D 数组以便我放下碎片的地方用其他颜色填充时会抛出"线程中的异常"AWT-EventQueue-0"java.lang.ArrayIndexOutOfBoundsException:6"。我尝试将其放在不同的地方并以其他顺序调用它,但它总是引发此异常

编辑:这只是一个原型代码,需要做更多的工作,但我需要处理这个错误才能继续。

全栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 6
    at GameLoop$DrawBoard.paintComponent(GameLoop.java:69)
    at javax.swing.JComponent.paint(JComponent.java:1056)
    at javax.swing.JComponent.paintChildren(JComponent.java:889)
    at javax.swing.JComponent.paint(JComponent.java:1065)
    at javax.swing.JComponent.paintChildren(JComponent.java:889)
    at javax.swing.JComponent.paint(JComponent.java:1065)
    at javax.swing.JLayeredPane.paint(JLayeredPane.java:586)
    at javax.swing.JComponent.paintChildren(JComponent.java:889)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5217)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1579)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1502)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
    at javax.swing.JComponent.paint(JComponent.java:1042)
    at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:39)
    at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:79)
    at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:116)
    at java.awt.Container.paint(Container.java:1975)
    at java.awt.Window.paint(Window.java:3904)
    at javax.swing.RepaintManager$4.run(RepaintManager.java:842)
    at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
    at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
/**
 * Author : Matija & Zvonimir 
 * Version : 1.0
 * Purpose : Connect 4 game work. Creating working game with arrays
 *           
 *
*/
import java.util.Arrays;
import java.util.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.JComponent;
import java.awt.Graphics2D;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
public class GameLoop extends JFrame{
   // attributes
   private int [][] matrix;
   int col;
   int row;
   private boolean gameOver = false;
   private int playerTurn = 0;
   Scanner scan = new Scanner(System.in);
   //constants
   private static final int COL = 7;
   private static final int ROWS = 6;
   public static final int SQUARE_SIZE = 100;
   public static final int WIDTH = COL * SQUARE_SIZE;
   public static final int HEIGHT = (ROWS+1) * SQUARE_SIZE;
   public static final int CIRCLE_WIDTH_HEIGHT = (SQUARE_SIZE) - 15;
   //MAIN METHOD
   public static void main(String []args){      
      GameLoop gl = new GameLoop();
      //gl.gameOver();
   }//main
   /*GRAPHICS*/  
   private class DrawBoard extends JPanel{      
      int[][] board = new int[ROWS][COL];
      public DrawBoard(){
         for(int i = 0; i < ROWS; i++){
            for(int j = 0; j < COL; j++){
               board[i][j] = 0;
            }         
         }//set to 0;
      }      

      public void paintComponent(Graphics g){
         super.paintComponents(g); //override  
         for(int i = 0; i < COL; i++){
            for(int j = 0; j < ROWS; j++){
               g.setColor(Color.BLUE);
               g.fillRect( (i*SQUARE_SIZE),(j * SQUARE_SIZE+SQUARE_SIZE),SQUARE_SIZE,SQUARE_SIZE);     
               if(board[i][j] == 0){
                  g.setColor(Color.BLACK);
                  g.fillOval( (i*SQUARE_SIZE),(j*SQUARE_SIZE +SQUARE_SIZE),CIRCLE_WIDTH_HEIGHT,CIRCLE_WIDTH_HEIGHT);                  
               }else if(board[i][j] == 1){
                  g.setColor(Color.RED);
                  g.fillRect( (i*SQUARE_SIZE),(j * SQUARE_SIZE+SQUARE_SIZE),SQUARE_SIZE,SQUARE_SIZE);                     
               }else if(board[i][j] == 2){ 
                  g.setColor(Color.YELLOW);
                  g.fillOval( (i*SQUARE_SIZE),(j*SQUARE_SIZE +SQUARE_SIZE),CIRCLE_WIDTH_HEIGHT,CIRCLE_WIDTH_HEIGHT);
               }
            }
         }     
         for(int i = 0; i < COL; i++){
            for(int j = 0; j < ROWS; j++){

            }//for J
         } //for I
      }//paint component method  
   }//CLASS

   public GameLoop(){            
      DrawBoard board = new DrawBoard();      
      add(board);
      setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
      setPreferredSize( new Dimension(701,732));
      //setResizable(false);
      setLocation( 400,200 );
      setVisible ( true );
      pack();    
   }
   /*
    * Method that creates a 2d array (matrix) filled with 0s
    * @param matrix 2d array
    */ 
   public int[][] createBoard(){
      matrix = new int[ROWS][COL];
      for(int i = 0; i < ROWS; i++){
         for(int j = 0; j < COL; j++){
            matrix[i][j] = 0;
         }         
      }//set to 0
      return matrix;
   }//create board       

}//class game loop

索引板时混合了行和列[]

     for(int i = 0; i < COL; i++){
        for(int j = 0; j < ROWS; j++){
           g.setColor(Color.BLUE);
           g.fillRect( (i*SQUARE_SIZE),(j * SQUARE_SIZE+SQUARE_SIZE),SQUARE_SIZE,SQUARE_SIZE);     
           if(board[i][j] == 0){
              g.setColor(Color.BLACK);
              g.fillOval( (i*SQUARE_SIZE),(j*SQUARE_SIZE +SQUARE_SIZE),CIRCLE_WIDTH_HEIGHT,CIRCLE_WIDTH_HEIGHT);                  
           }else if(board[i][j] == 1){
              g.setColor(Color.RED);
              g.fillRect( (i*SQUARE_SIZE),(j * SQUARE_SIZE+SQUARE_SIZE),SQUARE_SIZE,SQUARE_SIZE);                     
           }else if(board[i][j] == 2){ 
              g.setColor(Color.YELLOW);
              g.fillOval( (i*SQUARE_SIZE),(j*SQUARE_SIZE +SQUARE_SIZE),CIRCLE_WIDTH_HEIGHT,CIRCLE_WIDTH_HEIGHT);
           }
        }
     }  

它应该是 board[j][i],或者只是在循环语句中交换它们的角色

相关内容

  • 没有找到相关文章

最新更新