您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 销售管理 > Java程序设计制作拼图游戏
Java程序设计实例:制作拼图游戏本例知识点一句话讲解新学知识使用Image类导入图片使用MouseEvent类处理鼠标事件已学知识使用Point类转换坐标使用Graphics类管理屏幕显示一、练习具体要求本练习制作拼图游戏,运行效果如图99-1所示。执行本实例后,用鼠标拖动选中的小图片进行拼接,可以拼成一幅完整的图片。本实例的知识点有:鼠标事件的监听,Graphics类和Image类的应用。二、程序及注释(1)编程思路:本练习因为要制作拼图游戏,所以首先要实现图片的导入。这是通过getImage()函数来实现的,该函数有两个参数,第一个参数指明图片的路径,第二个参数指明图片的名称。然后,因为要实现图片摆放的随意性,所以要通过initgame()函数来实现。Initgame()函数是自写函数,在函数体内,通过调用Math.random()函数产生随机数,用来达到图片位置摆放的随意性和随机性。最后,因为要实现人机交互.,所以首先要通过一系列函数来实现对鼠标事件的监听和响应,这是通过函数addMouseListener(this)和addMouseMotionListener(this)来完成的。这样程序会区分用户对鼠标不同的操作,正确执行相应的功能。(2)程序实现及注释:importjava.awt.*;importjava.applet.*;importjava.awt.event.*;publicclasspintuextendsAppletimplementsMouseListener,MouseMotionListener{privateImagepicture;privateGraphicsbuffer;privateImagepic[];privateImageoff_pic[];privateGraphicsoff_buf[];privateImageoff_screen;privateGraphicsoff_buffer;privateImageoff_drag;privateGraphicsoff_drag_buf;privateintmap[][];privateintran[];privateintwidth=0;privateintheight=0;privateintlastx;privateintlasty;privateintlast_downx;privateintlast_downy;privateintstepx;privateintstepy;privatebooleanchoose;privatebooleanclick[][];privatebooleanm_down;privatebooleanm_drag;privatebooleannot_redraw;privatebooleanable;Fontfont1,font2;//程序的初始化publicvoidinit(){resize(640,480);pic=newImage[3];off_pic=newImage[16];off_buf=newGraphics[16];map=newint[4][4];ran=newint[15];for(inta=0;a16;a++)map[a/4][a%4]=a;for(inta=0;a15;a++)ran[a]=a;click=newboolean[4][4];MediaTrackertracker=newMediaTracker(this);//要载入的图片pic[0]=getImage(getCodeBase(),PICTURE0.JPG);pic[1]=getImage(getCodeBase(),PICTURE1.JPG);pic[2]=getImage(getCodeBase(),PICTURE2.GIF);tracker.addImage(pic[0],0);tracker.addImage(pic[1],0);tracker.addImage(pic[2],0);try{tracker.waitForID(0);}catch(InterruptedExceptione){}//设置字体font1=newFont(TimesRoman,Font.BOLD,48);font2=newFont(TimesRoman,Font.BOLD,32);width=640;height=480;//初始化主界面initForm();//添加鼠标监听事件addMouseListener(this);addMouseMotionListener(this);}//面板初始化voidinitForm(){this.setBackground(Color.orange);if(off_drag==null){off_drag=createImage(width/4,height/4);off_drag_buf=off_drag.getGraphics();}}publicvoidpaint(Graphicsg){if(off_screen==null){off_screen=createImage(width,height);off_buffer=off_screen.getGraphics();}if(able){off_buffer.setColor(Color.black);for(inta=0;a4;a++)for(intb=0;b4;b++){if(map[a][b]!=15)off_buffer.drawImage(off_pic[map[a][b]],b*width/4,a*height/4,this);if(map[a][b]==15)off_buffer.fillRect(b*width/4,a*height/4,width/4,height/4);for(intc=0;c2;c++)off_buffer.drawRect(b*width/4+c,a*height/4+c,width/4-c,height/4-c);if(click[a][b]){off_buffer.setColor(Color.red);for(intd=0;d2;d++)off_buffer.drawOval(b*width/4-d,a*height/4-d,width/4+d,height/4+d);off_buffer.setColor(Color.black);}}}else{off_buffer.setColor(Color.orange);off_buffer.fillRect(0,0,640,480);off_buffer.setFont(font1);off_buffer.setColor(Color.red);off_buffer.drawImage(pic[2],30,50,250,180,this);off_buffer.drawImage(pic[0],370,160,250,180,this);off_buffer.drawImage(pic[1],60,270,250,180,this);off_buffer.drawString(ChooseOne!,320,100);}g.drawImage(off_screen,0,0,this);}publicvoidrepaint(){paint(this.getGraphics());}//单击鼠标时产生的事件publicvoidmouseClicked(MouseEventevt){}//鼠标进入某个区域时产生的事件publicvoidmouseEntered(MouseEventevt){}//鼠标退出某个区域时产生的事件publicvoidmouseExited(MouseEventevt){}//移动鼠标时产生的事件publicvoidmouseMoved(MouseEventevt){if(!able){Pointpoint;point=evt.getPoint();if(point.x30&&point.x280&&point.y50&&point.y230){off_buffer.setColor(Color.orange);off_buffer.fillRect(0,0,640,480);off_buffer.setFont(font1);off_buffer.drawImage(pic[2],25,45,250,180,this);off_buffer.drawImage(pic[0],370,160,250,180,this);off_buffer.drawImage(pic[1],60,270,250,180,this);off_buffer.setColor(Color.black);off_buffer.fillRect(30,225,250,5);off_buffer.fillRect(275,50,5,176);off_buffer.setColor(Color.red);off_buffer.drawString(picture2!,320,100);this.getGraphics().drawImage(off_screen,0,0,this);}elseif(point.x370&&point.x620&&point.y160&&point.y340){off_buffer.setColor(Color.orange);off_buffer.fillRect(0,0,640,480);off_buffer.setFont(font1);off_buffer.drawImage(pic[2],30,50,250,180,this);off_buffer.drawImage(pic[0],365,155,250,180,this);off_buffer.drawImage(pic[1],60,270,250,180,this);off_buffer.setColor(Color.black);off_buffer.fillRect(370,335,250,5);off_buffer.fillRect(615,160,5,175);off_buffer.setColor(Color.red);off_buffer.drawString(picture0!,320,100);this.getGraphics().drawImage(off_screen,0,0,this);}elseif(point.x60&&point.x310&&point.y270&&point.y450){off_buffer.setColor(Color.orange);off_buffer.fillRect(0,0,640,480);off_buffer.setFont(font1);off_buffer.drawImage(pic[2],30,50,250,180,this);off_buffer.drawImage(pic[0],370,160,250,180,this);off_buffer.drawImage(pic[1],55,265,250,180,this);off_buffer.setColor(Color.black);off_buffer.fillRect(60,445,250,5);off_buffer.fillRect(305,270,5,175);off_buffer.setColor(Color.red);off_buffer.drawString(picture1!,320,100);this.getGraphics().drawImage(off_screen,0,0,this);}else{repaint();}}}//拖动鼠标时产生的事件publicvoidmouseDragged(MouseEventevt){if(!able)return
本文标题:Java程序设计制作拼图游戏
链接地址:https://www.777doc.com/doc-5790434 .html