title: JAVA: I'm trying to make Connect4 game based on 2D array and graphics with swing
body: <p>Backend program works with scanner input and just printing out the matrix, but I have this problem with graphics. When I'm making just rectangles with ovals inside them works good, but when I introduce 2D array into the story so that where I drop the piece fills with other color throws "Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 6". I tried putting it in different places and calling it in other sequences but it always throws this exception</p>
  
  <p>EDIT: This is just a prototype code, much more work needs to be done, but I need to work this error to go on.</p>
  
  <p>Full stack trace: </p>
  
  <pre><code>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 &amp; 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 &lt; ROWS; i++){
              for(int j = 0; j &lt; COL; j++){
                 board[i][j] = 0;
              }         
           }//set to 0;
        }      
  
  
        public void paintComponent(Graphics g){
  
           super.paintComponents(g); //override  
  
           for(int i = 0; i &lt; COL; i++){
              for(int j = 0; j &lt; 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 &lt; COL; i++){
              for(int j = 0; j &lt; 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 &lt; ROWS; i++){
           for(int j = 0; j &lt; COL; j++){
              matrix[i][j] = 0;
           }         
        }//set to 0
  
        return matrix;
  
     }//create board       
  
  
  }//class game loop
  </code></pre>