问题遇到的现象和发生背景
java编写魔板游戏
问题相关代码,请勿粘贴截图
package ch7.data;
import javax.swing.*;
import java.awt.*;
public class Block extends JTextField{
Point point;
Object object;
Point [] [] allPoint;
int index_i,index_j;
public Block(){
setEditable(false);
setHorizontalAlignment(JTextField.CENTER);
setFont(new Font("Arial",Font.BOLD,16));
setForeground(Color.blue);
}
public void setAtPoint(Point p){
point = p;
}
public void setAllPoint(Point [] [] point){
allPoint = point;
}
public Point getAtPoint(){
return point;
}
public void setObject(Object object){
this.object = object;
if(object instanceof Integer){
Integer number = (Integer)object;
setText(""+number.intValue());
}
else if(object instanceof Image){
repaint();
}
}
public Object getObject(){
return object;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
int w=getBounds().width;
int h=getBounds().height;
try{
g.drawImage((Image)object,0,0,w,h,this);
}
catch(Exception exp){}
}
public boolean move(){
int m = -1,n=-1;
boolean successMove = false;
Point pStart = getAtPoint();
findIndex(pStart,allPoint);
for(int i = 0;i<allPoint.length;i++){
for(int j = 0;j<allPoint.length;i++){
if(!allPoint[i][j].isHaveBlock()){
m = i;
n = j;
}
}
}
if(Math.abs(index_i-m)+Math.abs(index_j-n) == 1){
this.setAtPoint(allPoint[m][n]);
successMove = true;
allPoint[m][n].setBlock(this);
allPoint[m][n].setHaveBlock(true);
pStart.setHaveBlock(false);
pStart.setBlock(null);
}
return successMove;
}
private void findIndex(Point p,Point[][] allPoint){
for(int i = 0;i<allPoint.length;i++){
for(int j = 0;j<allPoint[i].length;j++){
if(p == allPoint[i][j]){
index_i = i;
index_j = j;
break;
}
}
}
}
}
```java
package ch7.test;
import ch7.data.*;
public class AppTest{
public static void main(String [] args){
Point [] [] point = new Point[3][3];
for(int i=0;i<point.length;i++){
for(int j = 0;j<point[i].length;j++)
point[i][j] = new Point();
}
Block block [] [] = new Block[3][3];
for(int i=0;i<block.length;i++){
for(int j = 0;j<block[i].length;j++){
block[i][j] = new Block();
block[i][j].setAllPoint(point);
block[i][j].setAtPoint(point[i][j]);
point[i][j].setHaveBlock(true);
point[i][j].setBlock(block[i][j]);
}
}
point[2][2].setHaveBlock(false);
point[2][2].setBlock(null);
VerifySuccess verifySuccess = new VerifySuccess();
Integer [] number = {1,2,3,4,5,6,7,8};
verifySuccess.setPoint(point);
verifySuccess.setObject(number);
block[0][0].setObject(number[0]);
block[0][1].setObject(number[1]);
block[0][2].setObject(number[2]);
block[1][0].setObject(number[3]);
block[1][1].setObject(number[7]);
block[1][2].setObject(number[4]);
block[2][0].setObject(number[6]);
block[2][1].setObject(number[5]);
intput(point);
System.out.println("----------------------------");
System.out.println("移动2次:");
System.out.println(point[2][1].getBlock().move());
System.out.println(point[1][1].getBlock().move());
intput(point);
System.out.println("成功否"+verifySuccess.isSuccess());
System.out.println("再移动2次:");
System.out.println(point[1][2].getBlock().move());
System.out.println(point[2][2].getBlock().move());
intput(point);
System.out.println("成功否"+verifySuccess.isSuccess());
}
static void intput(Point [] [] point){
int k = 0;
for(int i=0;i<point.length;i++){
for(int j=0;j<point[i].length;j++){
String s ="";
Block bk= point[i][j].getBlock();
if(bk!=null){
Integer object = (Integer)bk.getObject();
s =object.toString();
}
else
s ="#";
System.out.printf("%5s",s);
}
System.out.println();
}
}
}
运行结果及报错内容
报错: