Exception in thread "main"java.lang.ArrayIndexOutofBoundsException

leozhaodongdong 2012-05-18 01:04:09
这是我写的一个小拼图游戏 就是把0-8按顺序拼好 可是在DOS运行提示我14和34行Exception in thread "main"java.lang.ArrayIndexOutofBoundsException 求指点!不胜感激!

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class SlidingPuzzle implements MouseListener{
public static int level=3;
public PuzzleTile[][] XueButtonArray= new PuzzleTile[level-1][level-1];
JFrame Xueframe= new JFrame();
JPanel Xuepanel= new JPanel();

public static void main(String[] args){
SlidingPuzzle XueGame= new SlidingPuzzle();
XueGame.start();
}

public void start(){
Xueframe.setTitle("Numeric Slding Puzzle");
Container XueContainer=Xueframe.getContentPane();
XueContainer.setLayout(new GridLayout(level,level,1,1));

int[][] a=new int[level][level];
a[0][0]=(int)(Math.random()*(level*level));
this.XueButtonArray[0][0]=new PuzzleTile(a[0][0]);
this.XueButtonArray[0][0].setTileNumber(""+a[0][0]);
this.XueButtonArray[0][0].addMouseListener(this);
XueContainer.add(XueButtonArray[0][0]);
for(int i=0;i<level;i++){
for(int j=1;j<level;j++){
a[i][j]=(int)(Math.random()*(level*level));
do {
a[i][j]=(int)(Math.random()*(level*level));}while(a[i][j]==a[0][0]);
String tileNumber=""+a[i][j];
this.XueButtonArray[i][j]=new PuzzleTile(a[i][j]);
this.XueButtonArray[i][j].setTileNumber(tileNumber);
this.XueButtonArray[i][j].addMouseListener(this);
XueContainer.add(XueButtonArray[i][j]);
}
}
Xueframe.setSize(400, 400);
Xueframe.setResizable(false); // so that the frame size will not change
Xueframe.setVisible(true);
}

public void mouseEntered(MouseEvent e) {
PuzzleTile eButton = (PuzzleTile) e.getSource();
if (!eButton.getTileNumber().equals(""+0)) // change background color when mouse is over a tile
eButton.setBackground(Color.gray);
}

public void mousePressed(MouseEvent event) {
}
public void mouseReleased(MouseEvent event) {
}

public void mouseExited(MouseEvent e) {
PuzzleTile eButton = (PuzzleTile) e.getSource();
if (!eButton.getTileNumber().equals(""+0)) // change the color to its initial condition when mouse leave
eButton.setBackground(Color.darkGray);
}
public void mouseClicked(MouseEvent e) {
int[] XueNum=new int[4];
int i=0,j=0,n=0,m=0;
PuzzleTile eButton = (PuzzleTile)e.getSource(); // help to locate the button being clicked
if (eButton.getTileNumber().equals(""+0)) // see if it's the white color tile
; //nothing happens
else{
for (i = 0; i <level; i++) { // locate the position source button is in
for(j=0;j<level;j++){
if(this.XueButtonArray[i][j].getTileNumber().equals(eButton.getTileNumber())){
XueNum[0]=i;
XueNum[1]=j;
break;
}
}
}
for (n = 0; n <level; n++) { // locate the position white button is in
for(m=0;m<level;m++){
if(this.XueButtonArray[n][m].getTileNumber().equals(""+0)){
XueNum[2]=n;
XueNum[3]=m;
break;
}
}
}
/* Check the position relationship between white and source button.
If they are just next to each other, exchange states and call check() method. */
int CheckNum= Math.abs((XueNum[0]*level+XueNum[1])-(XueNum[2]*level+XueNum[3]));
if (CheckNum==1 || CheckNum==level){
this.XueButtonArray[XueNum[2]][XueNum[3]].setTileNumber(""+XueButtonArray[XueNum[0]][XueNum[1]].getTileNumber());
this.XueButtonArray[XueNum[2]][XueNum[3]].setBackground(Color.darkGray);
this.XueButtonArray[XueNum[0]][XueNum[1]].setTileNumber(""+level*level);
this.XueButtonArray[XueNum[0]][XueNum[1]].setBackground(Color.white);
this.check();
}
}
}
public void check(){
int checkNum=0;
for (int i = 0; i <level; i++) {
for(int j=0;j<level;j++){
if(XueButtonArray[i][j].getTileNumber().equals(""+(i*level+j)))
checkNum++; // check how many tiles are in the right position
}
}
if(checkNum==(level*level-1)){ // if all matches, open a window display congratulation message
int choiceEnd = JOptionPane.showConfirmDialog(null,"Would you like to Play again?","You have won!",JOptionPane.YES_NO_CANCEL_OPTION);
if (choiceEnd == JOptionPane.YES_OPTION){ // if user want to continue play a high level game
this.Xueframe.setVisible(false);
this.start();
}
if (choiceEnd == JOptionPane.NO_OPTION)
{
}
if (choiceEnd == JOptionPane.CANCEL_OPTION)
{
System.exit(0);
} // if user want to quit

}
}

}


import javax.swing.JPanel;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.FontMetrics;

/**
* A PuzzleTile class; this class inherits from JPanel.
*
* The tiles do not have any numbers painted on them yet.
* The only indication you will be able to visibly see at this point
* is that the 0 tile should be white and the rest gray.
*
* @author Laurissa
* @version 1.0
* @author Paula
* @version 1.1
*/

public class PuzzleTile extends JPanel {
private String tileNumber;

/**
* This constructor takes in the number you would like
* displayed on this panel and saves it as the String
* instance variable tileNumber.
* Note that you will still have to paint it onto the panel.
* You should do that by overriding the method:
* public void paintComponent(Graphics g){}.
*
* @param number The number to paint on the panel.
*/
public void paintComponent(Graphics g){
super.paintComponent(g);
Font myFont=new Font("Arial",Font.BOLD,30); // set font style
g.setFont(myFont);
FontMetrics myFm=g.getFontMetrics();
g.setColor(Color.white); //change color so that string displayed will be white
g.drawString(tileNumber,this.getWidth()/2-myFm.stringWidth(tileNumber)/2,this.getHeight()/2+myFm.getHeight()/2); //keep the string in the center position
}
public void setTileNumber(String tileNumber){
this.tileNumber = tileNumber;
this.repaint(); // invoke repaint() fuction to set the tile
}


/**
* This method returns tileNumber to SlidingPuzzle class to locate a PuzzleTile.
*
* @return tileNumber The number of PuzzleTile displays.
*/
public String getTileNumber(){
return tileNumber;
}

public PuzzleTile(int number) {
super();
if (number == 0) {
this.setBackground(Color.white);
}
else {
this.setBackground(Color.darkGray);
}
this.tileNumber = "" + number;
}

// You will need to provide more methods to interact with your
// main program.
}
...全文
14531 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
j_coffees 2012-05-18
  • 打赏
  • 举报
回复
public static int level=3;
public PuzzleTile[][] XueButtonArray= new PuzzleTile[level-1][level-1];
你定义了一个[2][2]的二维数组,
for(int i=0;i<level;i++)

循环的时候却有3个,当然数组下标越界
ladybirds2008 2012-05-18
  • 打赏
  • 举报
回复
越界。。
yjflinchong 2012-05-18
  • 打赏
  • 举报
回复
你的问题数组越界。 取值超出范围了。

我的问题:

————紧急求救~~~~~~~~CAS单点登录,退出之后用ticket参数访问抛出异常~~~~~ 求解决~~~~~

http://topic.csdn.net/u/20120518/09/7b84902e-4334-4886-b602-8bfa4a7f83ab.html
sffx123 2012-05-18
  • 打赏
  • 举报
回复
数组越界导致的,找到报错里面离你程序代码最近的位置看。
ChengLyn 2012-05-18
  • 打赏
  • 举报
回复
比较明显了,Exception in thread "main"java.lang.ArrayIndexOutofBoundsException,数组越界,你所期望取的那个数组位置不存在,譬如你整个数组大小也就[2,2]你却取了[2,3]或[-1,x].
这种异常就不帮你找哪里出错了, 果断debug一下吧,不出几分钟你就会找到问题原因了。
leozhaodongdong 2012-05-18
  • 打赏
  • 举报
回复
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

public class SlidingPuzzle implements MouseListener{
public static int level=3;
public PuzzleTile[][] XueButtonArray= new PuzzleTile[level-1][level-1];
JFrame Xueframe= new JFrame();
JPanel Xuepanel= new JPanel();

public static void main(String[] args){
SlidingPuzzle XueGame= new SlidingPuzzle();
XueGame.start();
}

public void start(){
Xueframe.setTitle("Numeric Slding Puzzle");
Container XueContainer=Xueframe.getContentPane();
XueContainer.setLayout(new GridLayout(level,level,1,1));

int[][] a=new int[level-1][level-1];


a[0][0]=(int)(Math.random()*(level*level));
a[1][0]=(int)(Math.random()*(level*level));
a[2][0]=(int)(Math.random()*(level*level));
if (a[0][0]!=a[1][0]&&a[0][0]!=a[2][0]&&a[1][0]!=a[2][0])
{this.XueButtonArray[0][0]=new PuzzleTile(a[0][0]);
this.XueButtonArray[0][0].setTileNumber(""+a[0][0]);
this.XueButtonArray[0][0].addMouseListener(this);
XueContainer.add(XueButtonArray[0][0]);
this.XueButtonArray[1][0]=new PuzzleTile(a[1][0]);
this.XueButtonArray[1][0].setTileNumber(""+a[1][0]);
this.XueButtonArray[1][0].addMouseListener(this);
XueContainer.add(XueButtonArray[1][0]);
this.XueButtonArray[2][0]=new PuzzleTile(a[2][0]);
this.XueButtonArray[2][0].setTileNumber(""+a[2][0]);
this.XueButtonArray[2][0].addMouseListener(this);
XueContainer.add(XueButtonArray[2][0]);
for(int i=0;i<level;i++){
for(int j=1;j<level;j++){
a[i][j]=(int)(Math.random()*(level*level));
do {
a[i][j]=(int)(Math.random()*(level*level));}while(a[i][j]==a[i][j-1]||a[i][j]==a[0][0]||a[i][j]==a[1][0]||a[i][j]==a[2][0]);
String tileNumber=""+a[i][j];
this.XueButtonArray[i][j]=new PuzzleTile(a[i][j]);
this.XueButtonArray[i][j].setTileNumber(tileNumber);
this.XueButtonArray[i][j].addMouseListener(this);
XueContainer.add(XueButtonArray[i][j]);
}
}
Xueframe.setSize(400, 400);
Xueframe.setResizable(false); // so that the frame size will not change
Xueframe.setVisible(true);
}
}

public void mouseEntered(MouseEvent e) {
PuzzleTile eButton = (PuzzleTile) e.getSource();
if (!eButton.getTileNumber().equals(""+0)) // change background color when mouse is over a tile
eButton.setBackground(Color.gray);
}

public void mousePressed(MouseEvent event) {
}
public void mouseReleased(MouseEvent event) {
}

public void mouseExited(MouseEvent e) {
PuzzleTile eButton = (PuzzleTile) e.getSource();
if (!eButton.getTileNumber().equals(""+0)) // change the color to its initial condition when mouse leave
eButton.setBackground(Color.darkGray);
}
public void mouseClicked(MouseEvent e) {
int[] XueNum=new int[4];
int i=0,j=0,n=0,m=0;
PuzzleTile eButton = (PuzzleTile)e.getSource(); // help to locate the button being clicked
if (eButton.getTileNumber().equals(""+0)) // see if it's the white color tile
; //nothing happens
else{
for (i = 0; i <level; i++) { // locate the position source button is in
for(j=0;j<level;j++){
if(this.XueButtonArray[i][j].getTileNumber().equals(eButton.getTileNumber())){
XueNum[0]=i;
XueNum[1]=j;
break;
}
}
}
for (n = 0; n <level; n++) { // locate the position white button is in
for(m=0;m<level;m++){
if(this.XueButtonArray[n][m].getTileNumber().equals(""+0)){
XueNum[2]=n;
XueNum[3]=m;
break;
}
}
}
/* Check the position relationship between white and source button.
If they are just next to each other, exchange states and call check() method. */
int CheckNum= Math.abs((XueNum[0]*level+XueNum[1])-(XueNum[2]*level+XueNum[3]));
if (CheckNum==1 || CheckNum==level){
this.XueButtonArray[XueNum[2]][XueNum[3]].setTileNumber(""+XueButtonArray[XueNum[0]][XueNum[1]].getTileNumber());
this.XueButtonArray[XueNum[2]][XueNum[3]].setBackground(Color.darkGray);
this.XueButtonArray[XueNum[0]][XueNum[1]].setTileNumber(""+level*level);
this.XueButtonArray[XueNum[0]][XueNum[1]].setBackground(Color.white);
this.check();
}
}
}
public void check(){
int checkNum=0;
for (int i = 0; i <level; i++) {
for(int j=0;j<level;j++){
if(XueButtonArray[i][j].getTileNumber().equals(""+(i*level+j)))
checkNum++; // check how many tiles are in the right position
}
}
if(checkNum==(level*level-1)){ // if all matches, open a window display congratulation message
int choiceEnd = JOptionPane.showConfirmDialog(null,"Would you like to Play again?","You have won!",JOptionPane.YES_NO_CANCEL_OPTION);
if (choiceEnd == JOptionPane.YES_OPTION){ // if user want to continue play a high level game
this.Xueframe.setVisible(false);
this.start();
}
if (choiceEnd == JOptionPane.NO_OPTION)
{
}
if (choiceEnd == JOptionPane.CANCEL_OPTION)
{
System.exit(0);
} // if user want to quit

}
}

}










又改了改SlidingPuzzle class 还是不对 = =

58,453

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧