



下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、项目效果图:源代码:importimportimportimportimportimportimportimportimportimportimportimportimportimportimportimportimportimportimportimportjava.awt.Color;java.awt.Container;java.awt.Dimension;java.awt.Graphics;java.awt.Graphics2D;java.awt.Point;java.awt.event.ComponentAdapter;java.awt.event.ComponentEvent;ja
2、va.awt.event.KeyAdapter;java.awt.event.KeyEvent;java.awt.event.MouseAdapter;java.awt.event.MouseEvent;java.awt.event.MouseMotionAdapter;java.awt.event.WindowAdapter;java.awt.event.WindowEvent;java.awt.geom.Rectangle2D;java.util.ArrayList;java.util.HashMap;java.util.Map;java.util.*;importimportimport
3、importjavax.swing.JFrame;javax.swing.JLabel;javax.swing.JOptionPane;javax.swing.JPanel;/*author:蒋冰*/publicclassKeyBoardextendsJFrameprivateintdirection=0;privateintjudge=0;privatePanelpanel=newPanel();privateRectLabellabel=newRectLabel1200;Integer>();privateHashMap<Integer,Integer>map=newHa
4、shMap<Integer,privateArrayList<RectLabel>list=newArrayList<RectLabel>();privatePointpoint=newPoint1200;privateinttargetX=0;privateinttargetY=0;privatestaticintgoal=3;privateintspeed;privateRectLabeltargetLabel=newRectLabel();privatestaticKeyBoardframe=newKeyBoard();publicstaticvoidmai
5、n(String口args)frame.setVisible(true);publicKeyBoard()super();addWindowListener(newWindowAdapter()OverridepublicvoidwindowActivated(WindowEventarg0)map.put(KeyEvent.map.put(KeyEvent.map.put(KeyEvent.map.put(KeyEvent.map.put(0,0);map.put(KeyEvent.VK_RIGHT,KeyEvent.VK_LEFT);VK_LEFT,KeyEvent.VK_RIGHT);V
6、K_UPKeyEvent.VK_DOWNVK_DOWNKeyEvent.VK_UP);VK_G0);Randomra=newRandom();targetX=ra.nextInt(30)*10+50;targetY=ra.nextInt(20)*10+50;targetLabel.setLocation(targetXtargetY););setBackground(Color.gray);setLayout(null);setBounds(400,200,400,300);addKeyListener(newKeyAdapter()publicvoidkeyPressed(KeyEvente
7、)if(e.getKeyCode()=map.get(direction)return;elsedirection=e.getKeyCode(););for(inti=1;i<goal;i+)labeli=newRectLabel();labeli.setBounds(-10,-10,10,10);list.add(labeli);panel.setBounds(-10,-10,10,10);panel.addComponentListener(newComponentAdapter()OverridepublicvoidcomponentMoved(ComponentEventarg0
8、)point0=panel.getLocation();if(Math.abs(point0.x-targetX)<5&&Math.abs(point0.y-targetY)<5)Randomra=newRandom();targetX=ra.nextInt(30)*10+50;targetY=ra.nextInt(20)*10+50;while(true)for(inti=1;i<goal-1;i+)Pointp=labeli.getLocation();if(p.x=targetX)&&(p.y=targetY)break;elsejudg
9、e+;if(judge=goal-2)break;elsejudge=0;targetX=ra.nextInt(30)*10+50;targetY=ra.nextInt(20)*10+50;targetLabel.setLocation(targetX,targetY);labelgoal=newRectLabel();labelgoal.setBounds(-10,-10,10,10);pointgoal-1=newPoint();list.add(labelgoal);goal+;for(inti=1;i<list.size();i+)pointi=labeli.getLocatio
10、n();labeli.setLocation(pointi-1);frame.add(labeli);)labellist.size().setLocation(pointlist.size()-1);frame.add(labellist.size();for(inti=3;i<list.size();i+)if(Math.abs(label1.getLocation().x-labeli.getLocation().x)<=1&&Math.abs(label1.getLocation().y-labeli.getLocation().y)<=1)direc
11、tion=KeyEvent.VK_G;);targetLabel.setBounds(200,100,10,10);add(targetLabel);add(panel);classPanelextendsJPanelimplementsRunnableprivateintflag=0;privateintx=0;privateinty=0;publicPanel()setPreferredSize(newDimension。,10);newThread(this).start();Overridepublicvoidrun()while(true)if(direction=KeyEvent.
12、VK_RIGHT)if(x<=this.getParent().getWidth()-10)x+=10;setLocation(x,y);elsedirection=KeyEvent.VK_G)if(direction=KeyEvent.VK_G)JOptionPane.showMessageDialog(null"+(goal-3)+"分,"+"欢迎再次光临!");System.exit(0);setLocation(-10,-10);)if(direction=KeyEvent.VK_LEFT)if(x>=0)x-=10;set
13、Location(x,y);elsedirection=KeyEvent.VK_Gif(direction=KeyEvent.VK_DOWNif(y<=this.getParent().getHeight()-10)y+=10;setLocation(x,y);elsedirection=KeyEvent.VK_G;if(direction=KeyEvent.VK_UF)if(y>=0)y-=10;setLocation(x,y);elsedirection=KeyEvent.VK_G;tryif(goal<30)speed=goal;elsespeed=30;Thread.sleep(400-speed*10);catch(InterruptedExceptione)e.printStackTrace();repaint();)classRectLabelextendsJLabelprivateintr=5;/小球半径privateintwidth=r*2;/球宽度privateintheight=r*2;/球高度privateColorballColor=Color.B
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 家乡美景的描绘写景作文(12篇)
- 2025年4月福建厦门市思明区部分单位联合招聘非在编人员考试3人考前自测高频考点模拟试题及答案详解参考
- 我的老师:一位充满智慧的人写人作文10篇
- 2025福建福州市事业单位招聘772人模拟试卷及参考答案详解
- 2025年菏泽市定陶区公开招聘教师(44人)考前自测高频考点模拟试题附答案详解(黄金题型)
- 2025届河南省豫西名校高三下学期模拟考试(一)英语试题(解析版)
- 2025江苏连云港市赣榆区事业单位招聘31人考前自测高频考点模拟试题及答案详解(夺冠)
- 2025年齐鲁医药学院公开招聘40名高层次人才(长期招聘岗位)模拟试卷含答案详解
- 2025河南周口市中医院招聘研究生117人模拟试卷及一套完整答案详解
- 青海省西宁市大通县2024-2025学年高一下学期期末地理试题(解析版)
- 2025贵州黔西南州民政局公益性岗位招聘模拟试卷及答案详解(典优)
- DHCP课件讲述教学课件
- 一国两制课件
- 隔震支座安装施工方案
- 中药生物安全培训内容课件
- 2024年武汉商学院公开招聘辅导员笔试题含答案
- 捶草印花课件
- vin码打印管理办法
- 银行反电诈培训课件
- tesol考试的样卷及答案
- DB32-T 5156-2025 零碳园区建设指南
评论
0/150
提交评论