人物拼图java_一个Java拼图游戏

1 importjava.awt.*;2 importjava.applet.*;3 importjava.awt.event.*;4 publicclassPPuzzleextendsApplet{5 Image imgPuzzle,buff;6 Point fifteen=newPoint(3,3);7 int[][] map={{0,4,8,12},{1,5,9,3},{2,6,10,14},{3,7,11,15}};8 intsx,sy;9 Canvas screen;10 Graphics gs,gb;11 booleanrunning=false;12 Button bStart=newButton("新游戏");13 Button bSee=newButton("显示正确的图像");14 publicvoidinit(){15 prepareImage();//准备图像16 sx=imgPuzzle.getWidth(this)/4;17 sy=imgPuzzle.getHeight(this)/4;18 setBackground(Color.blue);19 initScreen();//初始化画面20 initButtons();//初始化按钮21 add(screen);//向Applet添加组件22 add(bStart);23 add(bSee);24 }25 voidprepareImage(){//准备图像的方法26 imgPuzzle=getImage(getCodeBase(),"temp/xxx.jpg");27 MediaTracker mt=newMediaTracker(this);28 mt.addImage(imgPuzzle,0);//等待至图像全部加载完毕29 try{30 mt.waitForAll();31 }catch(Exception e){}32 //创建buffer并获取其Graphics对象33 buff=createImage(imgPuzzle.getWidth(this),imgPuzzle.getHeight(this));34 gb=buff.getGraphics();35 }36 voidinitMap(){//初始化map37 java.util.Random rnd=newjava.util.Random();38 inttemp,x1,y1,x2,y2;39 for(inti=0;i<100;i++){40 x1=rnd.nextInt(4);41 x2=rnd.nextInt(4);42 y1=rnd.nextInt(4);43 y2=rnd.nextInt(4);44 temp=map[x1][y1];45 map[x1][y1]=map[x2][y2];46 map[x2][y2]=temp;47 }48 outer:for(intj=0;j<4;j++)49 for(inti=0;i<4;i++)50 if(map[i][j]==15){51 fifteen.setLocation(i,j);52 breakouter;53 }54 }55 voidinitScreen(){//初始化画面56 screen=newCanvas(){//创建screen对象57 publicvoidpaint(Graphics g){//覆盖paint()方法58 if(gs==null)gs=getGraphics();//获取screen的Graphics对象59 if(running)60 drawScreen();61 else62 //若游戏未开始,则显示整幅图像63 g.drawImage(imgPuzzle,0,0,this);64 }65 };66 //设定screen的大小67 screen.setSize(imgPuzzle.getWidth(this),imgPuzzle.getHeight(this));68 69 screen.addMouseListener(newMouseAdapter(){//处理鼠标事件70 publicvoidmousePressed(MouseEvent me){71 if(!running)return;72 intx=me.getX()/sx,y=me.getY()/sy;73 intfx=(int)fifteen.getX(),fy=(int)fifteen.getY();74 if(Math.abs(fx-x)+Math.abs(fy-y)>=2)return;75 if(map[x][y]==15)return;76 map[fx][fy]=map[x][y];77 map[x][y]=15;78 fifteen.setLocation(x,y);79 drawScreen();80 }81 });82 }83 voidinitButtons(){//初始化按钮84 //“新游戏”按钮事件的处理85 bStart.addActionListener(newActionListener(){86 publicvoidactionPerformed(ActionEvent ae){87 initMap();//初始化mpa88 drawScreen();//绘制画面89 running=true;//代表游戏正在进行中90 bSee.setLabel("显示正确的图像");//改变bSee按钮的标题91 }92 });93 //“显示正确图像”按钮事件处理94 bSee.addActionListener(newActionListener(){95 publicvoidactionPerformed(ActionEvent ae){96 //bSee按钮标题为“继续游戏”97 if(bSee.getLabel().equals("继续游戏")){98 drawScreen();//绘制画面99 //bSee.setLabel("显示正确图像");//标题更改为“显示正确图像”100 }101 else{102 //bSee的标题为“显示正确图像”103 gs.drawImage(imgPuzzle,0,0,screen);//显示整幅图像104 bSee.setLabel("继续游戏");105 }106 }107 });108 }109 voiddrawScreen(){//绘制画面的方法110 gb.clearRect(0,0, sx*4, sy*4);//清空buffer111 //将制定位置的图像块绘制至buffer中112 inti;113 for(intj=0;j<4;j++)114 for(i=0;i<4;i++)115 if(map[i][j]!=15)drawSegment(map[i][j],i,j);116 //向Screen绘制buffer中的图像117 gs.drawImage(buff,0,0, screen);118 }119 voiddrawSegment(intseg,intx,inty){120 121 intdx=seg%4*sx,dy=seg/4*sy;122 123 gb.drawImage(imgPuzzle,x*sx,y*sy,x*sx+sx-1,y*sy+sy-1,dx,dy,dx+sx-1,dy+sy-1,screen);124 }125 126 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值