gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:15:24 26 4
gpt4 key购买 nike

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

编辑:这只是一个原型(prototype)代码,还需要做更多的工作,但我需要解决这个错误才能继续。

完整堆栈跟踪:

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],或者简单地在循环语句中交换它们的角色

关于JAVA:我正在尝试制作基于 2D 阵列和图形的 Connect4 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49316569/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com