读书人

ArrayIndexOutOfBoundsException解决方

发布时间: 2012-02-16 21:30:36 作者: rapoo

ArrayIndexOutOfBoundsException
//import java.lang.*;
import javax.microedition.lcdui.*;
//import java.util.Random;
//import javax.microedition.rms.*;
//import java.io.*;
//import MainMid;
//import MainPit.*;
//import com.*;
class MyCanvas extends Canvas implements Runnable
{
MyMIDlet myMid;
// 按键表
//private static final byte KEY_NONE = 0;
private static final byte KEY_UP = -1;
private static final byte KEY_DOWN = -2;
private static final byte KEY_LEFT = -3;
private static final byte KEY_RIGHT = -4;
private static final byte KEY_FIRE = -5;
private static final byte KEY_GAM_LEFT = -6;
private static final byte KEY_GAM_RIGHT = -7;
private static final byte KEY_NUM5 = 53;
private int hangfire = 300;// 延时大小
Graphics gb;
private Image bufImg;// 缓存
// 屏幕大小
private int nWidth;
private int nHeight;
/**
* 构造方法
* @param mid
*/
public MyCanvas(MyMIDlet mid)
{
myMid = mid;
nWidth = getWidth();// 屏幕大小
nHeight = getHeight();
cw = nWidth / 10;//把屏幕分成 10*10 的小格子
ch = nHeight / 10;
try
{
bufImg = Image.createImage(nWidth, nHeight);// 申请缓存空间
gb = bufImg.getGraphics();
} catch (Exception e)
{
System.out.println( "构造方法 ");
e.printStackTrace();
}
}
/**
* 刷新屏幕
* @param c
*/
private void clearScreen(int c)// 用颜色c刷新屏幕
{
gb.setColor(c);
gb.fillRect(0, 0, nWidth, nHeight);
}
/**
* 画笔
*/
public void paint(Graphics g){gb = g;}
private int cx, cy;//鼠标按下的时候 所在的起始点小方格的 行和列
/**
* 显示鼠标位置
*/
private void showCursor()
{
gb.setColor(0x000000ff);
gb.drawRect(cx * cw + 2, cy * ch + 2, cw - 4, ch - 4);
}
/**
* 按下起始位置
*
*/
private void showBegin()
{
gb.setColor(0x0000ff00);
gb.fillArc(begin_x * cw, begin_y * ch, cw, ch, 0, 360);
}
private int cw, ch;//每一个小格子的宽度和高度
/**
* 显示初始界面
*
*/
private void showMap()
{
try{
clearScreen(0x00ffffff);//背景色
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (moveSpace[i][j] == 1)//索引是 1 则绘制线条 否则填充障碍物


{
gb.setColor(0x00000000);//线条颜色
gb.drawRect(j * cw, i * ch, cw, ch);
} else
{
gb.setColor(0x00000000);//障碍颜色
gb.fillRect(j * cw, i * ch, cw, ch);
}
}
}
}catch(Exception e)
{
System.out.println( "showMap() error ");
e.printStackTrace();
}
}
int state = 0;
public void run()
{
while (true)
{
try{System.out.println( "state= "+state);
switch (state)
{
case 0://初始界面
showMap();//地图信息
showCursor();//鼠标位置
break;
case 1://开始按下
showMap();
showBegin();//起始点
showCursor();
break;
case 2://再次按下
showMap();
showBegin();
showfather(end_x, end_y);//寻路
showCursor();
break;
}
repaint();
serviceRepaints();
try
{
Thread.sleep(hangfire);
System.gc();
Thread.yield();
} catch (Exception e)
{
System.out.println( "线程 ");
e.printStackTrace();
}
}catch(Exception e)
{
System.out.println( "RUN 错误 at state= "+state);
e.printStackTrace();
}
}
}
/**
* 按键方法
*/
public void keyPressed(int keyCode)
{
try{
switch (keyCode)
{
/**
* 上下左右 为控制起始点的位置
*/
case KEY_UP:
if (cy > 0)
cy--;
break;
case KEY_DOWN:
if (cy < 9)
cy++;
break;
case KEY_LEFT:
if (cx > 0)
cx--;
break;
case KEY_RIGHT:
if (cx < 9)
cx++;
break;


case KEY_FIRE:
case KEY_GAM_LEFT:
case KEY_NUM5:
switch (state)
{
case 0:
begin_x = cx;
begin_y = cy;
state = 1;
break;
case 1:
end_x = cx;
end_y = cy;
state = 2;
AAsterisk();
break;
case 2:
state = 0;
break;
}
break;
case KEY_GAM_RIGHT:
myMid.exit();
break;
}

}catch(Exception e)
{
System.out.println( "keyPressed error ");
e.printStackTrace();
}
}

private int moveSpace[][] = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 255, 255, 255, 1, 1, 1, 1, 1 },
{ 1, 255, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 255, 1, 1, 1, 1, 1, 1, 1, 1 },


{ 255, 255, 255, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 255, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 255, 1, 1, 1, 1, 1, 1, 1 } };

private int openList[][] = new int[100][2];
private int openListLength = 0;
private int closeList[][] = new int[100][2];
private int closeListLength = 0;

private int mapInfo[][][] = new int[10][10][6];
private int begin_x = 0;
private int begin_y = 9;

private int end_x = 0;
private int end_y = 0;

private void addInOpenList(int x, int y)
{
try{
int temp[][] = openList;
openList = new int[openListLength+1][2];
for(int i = 0; i < openListLength;i++)
{
System.out.println( "openListLength= "+openListLength);
openList[0] = temp[0];
openList[1] = temp[1];
}
openList[openListLength][0] = x;
openList[openListLength][1] = y;
openListLength++;
}catch(Exception e)
{
System.out.println( "addInOpenList(int x, int y) error ");
e.printStackTrace();
}
}



[解决办法]
数组越界。建议查一下你用到的数组。

blog: java23.com/hb

读书人网 >J2ME开发

热点推荐