问题描述
importjava.awt.*;importjava.awt.event.KeyEvent;importjava.awt.event.KeyListener;importjava.util.Stack;importjavax.swing.JOptionPane;importjavax.swing.JPanel;classmainpanelextendsJPanelimplementsKeyListener{intmax;intmap[][];intmaptmp[][];intmanX;intmanY;intboxnum;ImagemyImage[];intlen;publicintlevel;Stackmystack;mainpanel(){max=50;len=30;level=1;mystack=newStack();setBounds(15,50,600,600);setBackground(Color.white);addKeyListener(this);myImage=newImage[10];for(inti=0;i<10;i++)myImage[i]=Toolkit.getDefaultToolkit().getImage((newStringBuilder()).append("pic\").append(i).append(".gif").toString());setVisible(true);}intmaxlevel(){returnmax;}publicvoidpaint(Graphicsg){for(inti=0;i<20;i++){for(intj=0;j<20;j++)g.drawImage(myImage[map[j][i]],i*len,j*len,this);}g.setColor(newColor(0,0,0));g.setFont(newFont("楷体_2312",1,30));g.drawString("现在是第",150,40);g.drawString(String.valueOf(level),310,40);g.drawString("关",360,40);}publicvoidkeyPressed(KeyEventkeyevent){if(keyevent.getKeyCode()==38)moveup();if(keyevent.getKeyCode()==40)movedown();if(keyevent.getKeyCode()==37)moveleft();if(keyevent.getKeyCode()==39)moveright();if(iswin()){if(level==max){JOptionPane.showMessageDialog(this,"恭喜您通过最后一关!!!");}else{Strings=(newStringBuilder()).append("恭喜您通过第").append(level).append("关!!!n是否要进入下一关?").toString();inti=0;Strings1="过关";intj=0;j=JOptionPane.showConfirmDialog(null,s,s1,i);if(j==1)System.exit(0);}mystack.removeAllElements();}}publicvoidkeyTyped(KeyEventkeyevent){}publicvoidkeyReleased(KeyEventkeyevent){}booleanisMystackEmpty(){returnmystack.isEmpty();}intback(){return((Integer)mystack.pop()).intValue();}voidremove(){mystack.removeAllElements();}voidmoveup(){if(map[manY-1][manX]==2||map[manY-1][manX]==4){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=4;elsemap[manY][manX]=2;map[manY-1][manX]=8;repaint();manY--;mystack.push(Integer.valueOf(10));}elseif(map[manY-1][manX]==3){if(map[manY-2][manX]==4){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=4;elsemap[manY][manX]=2;map[manY-1][manX]=8;map[manY-2][manX]=9;repaint();manY--;mystack.push(Integer.valueOf(11));}elseif(map[manY-2][manX]==2){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=4;elsemap[manY][manX]=2;map[manY-1][manX]=8;map[manY-2][manX]=3;repaint();manY--;mystack.push(Integer.valueOf(11));}else{map[manY][manX]=8;repaint();}}elseif(map[manY-1][manX]==9)if(map[manY-2][manX]==4){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=4;elsemap[manY][manX]=2;map[manY-1][manX]=8;map[manY-2][manX]=9;repaint();manY--;mystack.push(Integer.valueOf(11));}elseif(map[manY-2][manX]==2){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=4;elsemap[manY][manX]=2;map[manY-1][manX]=8;map[manY-2][manX]=3;repaint();manY--;mystack.push(Integer.valueOf(11));}else{map[manY][manX]=8;repaint();}if(map[manY-1][manX]==1){map[manY][manX]=8;repaint();}}voidbackup(inti){intj=i;if(j==10){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=4;elsemap[manY][manX]=2;}elseif(j==11){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=9;elsemap[manY][manX]=3;if(maptmp[manY-1][manX]==4||maptmp[manY-1][manX]==9)map[manY-1][manX]=4;elsemap[manY-1][manX]=2;}map[manY+1][manX]=8;repaint();manY++;}voidmovedown(){if(map[manY+1][manX]==2||map[manY+1][manX]==4){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=4;elsemap[manY][manX]=2;map[manY+1][manX]=5;repaint();manY++;mystack.push(Integer.valueOf(20));}elseif(map[manY+1][manX]==3){if(map[manY+2][manX]==4){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=4;elsemap[manY][manX]=2;map[manY+1][manX]=5;map[manY+2][manX]=9;repaint();manY++;mystack.push(Integer.valueOf(21));}elseif(map[manY+2][manX]==2){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=4;elsemap[manY][manX]=2;map[manY+1][manX]=5;map[manY+2][manX]=3;repaint();manY++;mystack.push(Integer.valueOf(21));}else{map[manY][manX]=5;repaint();}}elseif(map[manY+1][manX]==9){if(map[manY+2][manX]==4){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=4;elsemap[manY][manX]=2;map[manY+1][manX]=5;map[manY+2][manX]=9;repaint();manY++;mystack.push(Integer.valueOf(21));}elseif(map[manY+2][manX]==2){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=4;elsemap[manY][manX]=2;map[manY+1][manX]=5;map[manY+2][manX]=3;repaint();manY++;mystack.push(Integer.valueOf(21));}else{map[manY][manX]=5;repaint();}}elseif(map[manY+1][manX]==1){map[manY][manX]=5;repaint();}}voidmoveleft(){if(map[manY][manX-1]==2||map[manY][manX-1]==4){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=4;elsemap[manY][manX]=2;map[manY][manX-1]=6;repaint();manX--;mystack.push(Integer.valueOf(30));}elseif(map[manY][manX-1]==3){if(map[manY][manX-2]==4){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=4;elsemap[manY][manX]=2;map[manY][manX-1]=6;map[manY][manX-2]=9;repaint();manX--;mystack.push(Integer.valueOf(31));}elseif(map[manY][manX-2]==2){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=4;elsemap[manY][manX]=2;map[manY][manX-1]=6;map[manY][manX-2]=3;repaint();manX--;mystack.push(Integer.valueOf(31));}else{map[manY][manX]=6;repaint();}}elseif(map[manY][manX-1]==9){if(map[manY][manX-2]==4){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=4;elsemap[manY][manX]=2;map[manY][manX-1]=6;map[manY][manX-2]=9;repaint();manX--;mystack.push(Integer.valueOf(31));}elseif(map[manY][manX-2]==2){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=4;elsemap[manY][manX]=2;map[manY][manX-1]=6;map[manY][manX-2]=3;repaint();manX--;mystack.push(Integer.valueOf(31));}else{map[manY][manX]=6;repaint();}}elseif(map[manY][manX-1]==1){map[manY][manX]=6;repaint();}}voidbackleft(inti){intj=i;if(j==30){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=4;elsemap[manY][manX]=2;}elseif(j==31){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=9;elsemap[manY][manX]=3;if(maptmp[manY][manX-1]==4||maptmp[manY][manX-1]==9)map[manY][manX-1]=4;elsemap[manY][manX-1]=2;}map[manY][manX+1]=6;repaint();manX++;}voidbackright(inti){intj=i;if(j==40){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=4;elsemap[manY][manX]=2;}elseif(j==41){if(maptmp[manY][manX]==4||maptmp[manY][manX]==9)map[manY][manX]=9;elsemap[manY][manX]=3;if(maptmp[manY][manX+1]==4||maptmp[manY][manX+1]==9)map[manY][manX+1]=4;elsemap[manY][manX+1]=2;}map[manY][manX-1]=7;repaint();manX--;}booleaniswin(){booleanflag=false;label0:for(inti=0;i<20;i++){intj=0;do{if(j>=20)continuelabel0;if(maptmp[i][j]==4||maptmp[i][j]==9)if(map[i][j]==9){flag=true;}else{flag=false;breaklabel0;}j++;}while(true);}returnflag;}publicstaticvoidmain(Stringargs[]){newmainpanel();}}
解决方案
解决方案二:
因为没有用上泛型
解决方案三:
jdk版本问题
解决方案四:
stack没用泛型可以用参数javac-Xlint编译