




免费预览已结束,剩余13页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
JAVA实验报告 班级: 24021002 学号:2402100210 姓名: 黄小君1. 题目红绿灯控制道路车辆2.设计思路交通红绿灯是交通的控制中心,控制着道路上的所有车辆的行进与停止,各各车辆为各自的线程,红绿灯也是一个线程,同时,红绿灯线程用控制着车子线程的run、suspend、resume。3重点代码解释 (1)此处代码为添加背景音乐及建立一个JPanel容器File musicFile;/URI uri;URL url;public void init() musicFile=new File(街道上汽车鸣笛的声音.wav);uri=musicFile.toURI();tryurl=uri.toURL();catch(Exception e)AudioClip clip=JApplet.newAudioClip(url);clip.loop();/以上为添加音乐 Container cp=getContentPane(); CBox pa=new CBox(); pa.setPreferredSize(new Dimension(1000, 1000);pa.setBackground(Color.red); cp.add(pa,BorderLayout.CENTER);/建立一个JPanel容器 (2)此段代码为红绿灯线程,控制红绿灯的变化,同时还会在变化灯时唤醒其他的线程,使正在sleep的线程resume。while(th5=Thread.currentThread()/线程5,此线程控制红绿灯 if(L1.RedLight) th3.resume();/当红灯亮时,东西向车子停,南北向车子动 try th5.sleep(5000); L1.RedLight=false; L1.YellowLight=true; L1.GreenLight=false; repaint(); catch(Exception e) if(L1.YellowLight) th3.resume();/当黄灯亮时,东西向车子停,南北向车子动 try th5.sleep(5000); L1.YellowLight=false; L1.GreenLight=true; L1.RedLight=false; repaint(); catch(Exception e) if(L1.GreenLight) th1.resume();/当绿灯亮时,东西向车子启动,南北向车子停止 th2.resume(); th4.resume(); try th5.sleep(10000); L1.YellowLight=false; L1.RedLight=true; L1.GreenLight=false; repaint(); catch(Exception e) (3)我们在屏幕上自绘图形或者是添加图片都是要通过所在画布的重绘来实现的,因此闪烁的出现必然与重绘机制有着一些关联。在awt中对于窗体画布的重绘其条用顺序是repaint() update()paint()。update中有一个清屏的作用,即g.clearRect(0, 0, width, height);然后再在下面调用paint(g),函数进行重绘。 public void update(Graphics g) /双缓冲原理实现消除闪烁 Image offScreenImage = null; if (offScreenImage = null) offScreenImage = this.createImage(2000, 2000); Graphics g1 = offScreenImage.getGraphics(); Color c = Color.RED; g1 .setColor(c); g1 .fillRect(0, 0, 2000, 1000); paint(g1); g1.drawImage(offScreenImage, 0, 0, null); (4)由各各类的对象调用类的paint方法,画出图形。public void paint(Graphics g) super.paint(g); Graphics2D g2=(Graphics2D)g;setBackground(Color.red); g2.setStroke(new BasicStroke(2); r1.paint(g2);/画道路r1 c1.paintCar1(g2);/画车c1 c2.paintCar2(g2);/画车c2 c3.paintCar3(g2);/画车c3 c4.paintCar4(g2);/画车c4 L1.paintLight(g2);/画路灯L1 (5)在package tool中编写road类,并import到default包中,以便在finalwork中调用。代码略(6)在package tool中编写car类,并import到default包中,以便在finalwork中调用。代码略(7)在package tool中编写light类,并import到default包中,以便在finalwork中调用。代码略4.部分截屏5.总结 最近一段时间的java结业设计,是我们这学期所学内容的一个系统性总结。通过这次的设计,让我明白了很多的东西。一是课业知识的不足,在做程序时我遇到了很多的技术性难题,是我们以前都没有遇到过的,即使是在看几遍书的情况下还是不能很好的运用,让我了解到自己再java方面的很多知识漏洞。二是运用API文档的意识不强,在遇到问题时,总不能在第一时间里查看API文档来解决问题,而是很盲目的通过其他的途径寻找解决办法,却不知最好的帮手就在自己的身边。 在以后的学习中一定要注意这次所没有注意到的问题,平时认真的学好专业知识,并不断的通过课外途径充实自己在课堂上所学知识的不足之处,课程学习达到精益求精。附:全部代码(1)finalwork类代码:import java.applet.Applet;import java.applet.AudioClip;import java.awt.*;import java.io.File;import .URI;import .URL;import javax.swing.*;import tool.car;import tool.light;import tool.road;SuppressWarnings( serial, unused )public class finalwork extends JApplet File musicFile;/URI uri;URL url;public void init() musicFile=new File(街道上汽车鸣笛的声音.wav);uri=musicFile.toURI();tryurl=uri.toURL();catch(Exception e)AudioClip clip=JApplet.newAudioClip(url);clip.loop();/以上为添加音乐 Container cp=getContentPane(); CBox pa=new CBox(); pa.setPreferredSize(new Dimension(1000, 1000);pa.setBackground(Color.lightGray); cp.add(pa,BorderLayout.CENTER);/建立一个JPanel容器SuppressWarnings(serial)class CBox extends JPanel implements Runnable road r1=new road();/申请road类的对象r1car c1=new car();/申请car类的对象c1car c2=new car();/申请类car类的对象c2car c3=new car();/申请类car的对象c3car c4=new car();/申请类car的对象c4light L1=new light();/申请light类的对象L1 Thread th1=null,th2=null,th3=null,th4=null,th5=null;public CBox()th1=new Thread(this);th1.start();th2=new Thread(this);th2.start();th3=new Thread(this);th3.start();th4=new Thread(this);th4.start();th5=new Thread(this);th5.start(); /线程初始化public void stop() th1=null; th2=null; th3=null; th4=null; th5=null; /线程的stop() SuppressWarnings(deprecation)public void run()/线程的run()方法 Thread current=Thread.currentThread(); while(th1=current) /线程1,控制西向红车 if(L1.YellowLight|L1.RedLight)&c1.x60700) th1.suspend();/当路灯显示为红灯或黄灯,且在斑马线附近时,此线程suspend,车子停止 else if(L1.GreenLight) th1.resume(); /在灯变绿灯时,此线程在线程5中resume if(c1.x64=0) c1.x60=1620;c1.x61=1624;c1.x62=1640;c1.x63=1670; c1.x64=1674;c1.x65=1680;c1.x66=1620; repaint(); else for(int j=0;jc1.x6.length;j+) c1.x6j=c1.x6j-20; c1.e41=c1.e41-4; c1. e51=c1.e51-4; repaint(); try th1.sleep(100); catch(InterruptedException e) while(th2=current) /线程2,控制西向绿车 if(L1.YellowLight|L1.RedLight)&c2.x70700) th2.suspend();/同th1 else if(L1.GreenLight) th2.resume(); if(c2.x740) c2.x70=2020;c2.x71=2024;c2.x72=2040;c2.x73=2070; c2.x74=2074;c2.x75=2080;c2.x76=2020; else for(int j=0;j=351&c3.yy0=407) th3.suspend(); if(c3.yy0=398-60&c4.xx01400) c4.xx0=-150; else c4.xx0=c4.xx0+5; repaint(); try th4.sleep(100); catch(InterruptedException e) while(th5=Thread.currentThread()/线程5,此线程控制红绿灯 if(L1.RedLight) th3.resume();/当红灯亮时,东西向车子停,南北向车子动 try th5.sleep(5000); L1.RedLight=false; L1.YellowLight=true; L1.GreenLight=false; repaint(); catch(Exception e) if(L1.YellowLight) th3.resume();/当黄灯亮时,东西向车子停,南北向车子动 try th5.sleep(5000); L1.YellowLight=false; L1.GreenLight=true; L1.RedLight=false; repaint(); catch(Exception e) if(L1.GreenLight) th1.resume();/当绿灯亮时,东西向车子启动,南北向车子停止 th2.resume(); th4.resume(); try th5.sleep(10000); L1.YellowLight=false; L1.RedLight=true; L1.GreenLight=false; repaint(); catch(Exception e) public void update(Graphics g) /双缓冲原理实现消除闪烁 Image offScreenImage = null; if (offScreenImage = null) offScreenImage = this.createImage(2000, 2000); Graphics g1 = offScreenImage.getGraphics(); Color c = Color.black; g1 .setColor(c); g1 .fillRect(0, 0, 2000, 1000); paint(g1); g1.drawImage(offScreenImage, 0, 0, null); public void paint(Graphics g)super.paint(g); Graphics2D g2=(Graphics2D)g;g2.setBackground(Color.lightGray); g2.setStroke(new BasicStroke(2); r1.paint(g2);/画道路r1 c1.paintCar1(g2);/画车c1 c2.paintCar2(g2);/画车c2 c3.paintCar3(g2);/画车c3 c4.paintCar4(g2);/画车c4 L1.paintLight(g2);/画路灯L1(2)road类代码:package tool;import java.awt.BasicStroke;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.geom.GeneralPath;import java.awt.geom.Line2D;public class road /public int x1=0,535,535,695,695,1462,1462,695,695,535,535,0,0;public int y1=162,162,0,0,162,162,322,322,690,690,322,322,162;/用数组存储画道路的各点坐标Font f1=new Font(隶书,Font.BOLD,48);public void paint(Graphics2D g2)g2.setPaint(new Color(100,120,150);g2.setBackground(Color.green);g2.setStroke(new BasicStroke(4);GeneralPath polygon2=new GeneralPath(0,x1.length);polygon2.moveTo(x10,y10);for(int index=1;indexx1.length;index+)polygon2.lineTo(x1index, y1index);polygon2.closePath();g2.fill(polygon2);g2.setPaint(Color.yellow);g2.setStroke(new BasicStroke(6);Line2D.Double line1=new Line2D.Double(608,0,608,61);Line2D.Double line2=new Line2D.Double(819,242,1362,242);Line2D.Double line3=new Line2D.Double(608,407,608,660);Line2D.Double line4=new Line2D.Double(0,242,398,242);/路中间的黄线g2.draw(line1);g2.draw(line2);g2.draw(line3);g2.draw(line4);Line2D.Double zebraCrossing11=new Line2D.Double(535,61,695,61);Line2D.Double zebraCrossing12=new Line2D.Double(535,83,695,83);Line2D.Double zebraCrossing13=new Line2D.Double(535,109,695,109);/斑马线1Line2D.Double zebraCrossing21=new Line2D.Double(749,162,749,322);Line2D.Double zebraCrossing22=new Line2D.Double(785,162,785,322);Line2D.Double zebraCrossing23=new Line2D.Double(819,162,819,322);/斑马线2Line2D.Double zebraCrossing31=new Line2D.Double(535,355,695,355);Line2D.Double zebraCrossing32=new Line2D.Double(535,379,695,379);Line2D.Double zebraCrossing33=new Line2D.Double(535,407,695,407);/斑马线3Line2D.Double zebraCrossing41=new Line2D.Double(398,162,398,322);Line2D.Double zebraCrossing42=new Line2D.Double(427,162,427,322);Line2D.Double zebraCrossing43=new Line2D.Double(460,162,460,322);/斑马线4g2.setPaint(Color.white);g2.draw(zebraCrossing11);g2.draw(zebraCrossing12);g2.draw(zebraCrossing13);g2.draw(zebraCrossing21);g2.draw(zebraCrossing22);g2.draw(zebraCrossing23);g2.draw(zebraCrossing31);g2.draw(zebraCrossing32);g2.draw(zebraCrossing33);g2.draw(zebraCrossing41);g2.draw(zebraCrossing42);g2.draw(zebraCrossing43);g2.setPaint(Color.black);g2.setFont(f1);g2.drawString(2402100210, 800, 400);g2.drawString(黄小君,800,450);(3)car类代码:package tool;import java.awt.Color;import java.awt.Graphics2D;import java.awt.geom.Ellipse2D;import java.awt.geom.GeneralPath;import java.awt.geom.Rectangle2D;public class car public int x61=1120,x62=1124,x63=1140,x64=1170,x65=1174,x66=1180,x67=1120, y61=195,y62=185,y63=175,y64=175,y65=185,y66=195,y67=195, x6=x61,x62,x63,x64,x65,x66,x67, y6=y61,y62,y63,y64,y65,y66,y67,/车1 x71=1020,x72=1024,x73=1040,x74=1070,x75=1074,x76=1080,x77=1020, y71=225,y72=215,y73=205,y74=205,y75=215,y76=225,y77=225, x7=x71,x72,x73,x74,x75,x76,x77, y7=y71,y72,y73,y74,y75,y76,y77,/车2 e41=935, e42=190, e43=10, e44=10, e51=960,e52=190,e53=10,e54=10,/车1轮 e61=1035,e62=220,e63=10,e64=10, e71=1060,e72=220,e73=10,e74=10,/车2轮 x9=545+80,y9=420+500+200,w9=45,h9=60, yy=y9, x10=-60,y10=262,w10=60,h10=45, xx=x10;public void paintCar1(Graphics2D g2)Ellipse2D.Double e4=new Ellipse2D.Double(x65-16,y65,e43,e44);Ellipse2D.Double e5=new Ellipse2D.Double(x66+10,y66,e53,e54);g2.setPaint(Color.black);g2.fill(e4);g2.fill(e5);g2.setPaint(Color.red);GeneralPath polygon1=new GeneralPath(0,x6.length);polygon1.moveTo(x60, y60);for(int index=0;indexx6.length;index+)polygon1.lineTo(x6index, y6index);polygon1.closePath();g2.draw(polygon1);/车身public void paintCar2(Graphics2D g2)Ellipse2D.Double e6=new Ellipse2D.Double(x75-16,y75,e63,e64);Ellipse2D.Double e7=new Ellipse2D.Double(x76+10,y76,e73,e74);/车轮g2.setPaint(Color.black);g2.fill(e6);g2.fill(e7);g2.setPaint(Color.green);GeneralPath polygon2=new GeneralPath(0,x7.length);polygon2.moveTo(x70, y70);for(int index=0;indexx7.length;index+)polygon2.lineTo(x7index, y7index);polygon2.closePath();g2.draw(polygon2);/车身public vo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年上半征兵工作完成落后检讨表态发言
- 2025年山东省新闻系统事业单位人员招聘考试题库及答案解析
- 2025年武汉叉车考题题库及答案
- UOP催化工艺课件
- 2025年喉炎的试题及答案
- SPS脊柱训练课件
- SPSS-17.0医学统计课件
- 贵阳市2026届高三年级摸底考试生物试卷(含答案)
- 食品安全管理知识培训课件
- PS课件尺寸教学课件
- DL-T5366-2014发电厂汽水管道应力计算技术规程
- 石材厂设备保养操作手册
- 金融理财基础知识
- 送别混声合唱简谱
- 幼儿园分餐培训课件
- 化学在材料科学中的应用
- 高中物理知识模型探究与实践-电磁学篇
- 四年级下册递等式计算练习400道及答案
- 如何提高培智学校课堂教学的有效性
- 电工学(第8版)(上册 电工技术) 课件全套 秦曾煌 第1-14章 电路的基本概念与基本定律- 传感器
- 康复设备与康复仪器的康复设备与康复仪器
评论
0/150
提交评论