📄 mazemouse.java~14~
字号:
if (runFlag) {
if (doneFlag) { //判断是否运行结束
JOptionPane.showConfirmDialog(this,
" 算法演示完成!\n \n 单击相关按钮重新开始\n",
"演示完成", JOptionPane.DEFAULT_OPTION);
newButton.setEnabled(true);
stepButton.setEnabled(false);
stayButton.setEnabled(false);
runButton.setEnabled(false);
runFlag = false;
doneFlag=false;
stop();
}
else {
MazePath();
repaint();
controls = controlsPanel.delay;
try {
runner.sleep(controls);
}
catch (InterruptedException exp) {
exp.toString();
}
}
}
}
}
public void MazePath() //按照数据结构课本设计的
{
switch(codePart)
{
case 2:
codePanel.highlight(2);
mazeStack=new Stack();
//初始化开始点和结束点
curpos=new PosType(0,0);
end=new PosType(7,7);
codePart=3;
break;
case 3: //curstep
codePanel.highlight(3);
curstep=1;
codePart=5;
break;
case 5: //if(pass(curpos))
codePanel.highlight(5);
if(pass(curpos)) //可以通过返回true
{
codePart=6;
break;
}
else
{
codePart=12;
break;
}
case 6: //FootPrint(curpos)
codePanel.highlight(6);
pathflag[curpos.x][curpos.y]=true;
// button[curpos.x][curpos.y].setBackground(Color.red);
button[curpos.x][curpos.y].setIcon(mouseimg[1]);
codePart=7;
break;
case 7: //e=(curstep,curpos,1);
codePanel.highlight(7);
bankup=new SElemType();
bankup.setOrd(curstep);
bankup.setSeat(curpos);
bankup.setDi(1); //设置起始点方向
codePart=8;
break;
case 8: //
codePanel.highlight(8);
mazeStack.push(bankup); //保留进栈
codePart=9;
break;
case 9: //if(curpos==end)
codePanel.highlight(9);
if(curpos.x==end.x&&curpos.y==end.y) //结束
{
//结束程序Flag
searchpath=true;
codePart=22;
}
else
{
codePart=10;
}
break;
case 10: //curpos=next(curpos,1);
codePanel.highlight(10);
curpos=nextpos(curpos,1);
codePart=11;
break;
case 11: //curstep++
codePanel.highlight(11);
curstep++;
codePart=20;
break;
case 12: //else{
codePanel.highlight(12);
codePart=13;
break;
case 13: //if(!stackEmpty())
codePanel.highlight(13);
if(!mazeStack.empty())
{
codePart=14;
}
else
{
codePart=19;
}
break;
case 14: //pos(s,e);
codePanel.highlight(14);
e=(SElemType)mazeStack.pop();
codePart=15;
break;
case 15://while(e.di==4&&!stackEmpty(s))
codePanel.highlight(15);
if(e.di==4&&!mazeStack.empty())
{
codePart=16;
}
else
{
codePart=17;
}
break;
case 16: //markPrint(e.seat); pop(s,e)
codePanel.highlight(16);
markPrint(e.seat);
e=(SElemType)mazeStack.pop();
codePart=15;
break;
case 17: //if(e.di<4)
codePanel.highlight(17);
if(e.di<4)
{
codePart=18;
}
else
{
codePart=20;
}
break;
case 18: //e.di++; push(s,e);
codePanel.highlight(18);
e.di=e.di+1;
// if(curpos.x>0&&curpos.x<8&&curpos.y<8&&curpos.y>0)
button[e.seat.x][e.seat.y].setIcon(mouseimg[e.di]);
System.out.println("18hang "+e.di);
//////////
mazeStack.push(e);
codePart=19;
break;
case 19: //curpos=nextpos(e.seat,e.di);
codePanel.highlight(19);
curpos=nextpos(e.seat,e.di);
codePart=20;
break;
case 20: //while(!StackEmpty(s)
codePanel.highlight(20);
if(!mazeStack.empty())
{
codePart=5;
}
else
{
codePart=21;
}
break;
case 21: //return false
codePanel.highlight(21);
searchpath=false;
codePart=22;
break;
case 22: //{
codePanel.highlight(22);
doneFlag=true;
runFlag=true;
break;
}
}
public boolean pass(PosType seat) //
{
if(colorflag[seat.x+1][seat.y+1])
return false;
else
if(pathflag[seat.x][seat.y])
{ return false;}
else
return true;
}
public PosType nextpos(PosType curseat,int direction)
{
int x=0;
int y=0;
switch (direction)
{
case 1:
x=curseat.x;
y=curseat.y+1;
break;
case 2:
x=curseat.x+1;
y=curseat.y;
break;
case 3:
x=curseat.x;
y=curseat.y-1;
break;
case 4:
x=curseat.x-1;
y=curseat.y;
break;
}
return new PosType(x,y);
}
public void markPrint(PosType markseat) //留下脚印
{
button[markseat.x][markseat.y].setBackground(Color.BLUE);
colorflag[markseat.x+1][markseat.y+1]=true; //设置也不可以走过
}
class DealWith implements ActionListener //处理设置障碍类
{
public void actionPerformed(ActionEvent e)
{
if (!lockflag) {
JButton curbutton = (JButton) e.getSource();
int seat = Integer.parseInt(curbutton.getName());
int x = seat / 8;
int y = seat % 8;
if (!colorflag[x + 1][y + 1]) {
button[x][y].setBackground(Color.black);
colorflag[x + 1][y + 1] = true;
}
else {
button[x][y].setBackground(bgcolor);
colorflag[x + 1][y + 1] = false;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -