资源描述:
《udp——绘制温度曲线》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、[实验指导书]实验五、UDP——绘制温度曲线一、实验目的1.熟悉UDP编程的基本过程2.理解UDP协议相关的基本概念3.掌握UDP协议与线程的结合方式二、实验内容1.利用UDP协议实现一个简单的白板程序,要求如下:用户在客户端界面上单击鼠标左键后程序在鼠标单击位置画20*20的圆形,单击右键后程序在鼠标单击位置画20*20的圆。服务器界面上将同时显示与客户端一致的画面。2.采用UDP编写一个网络程序,该程序的客户端每隔1秒向服务端发送一个温度值,服务端能够接收该温度值并显示一条动态温度曲线。其界面如下:3.如果要求每增加一个客户端则增加一条动态曲线,并
2、且显示一条所有客户端的平均温度曲线,该程序应该如何改变?三、实验步骤1.针对第1题,定义数据传递的格式为:对象类型(圆、正方形等等)X坐标Y坐标1字节2字节2字节2.实现客户端绘制程序,其代码如下:packagelab.lab5;importjava.applet.*;importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjava.net.*;importjava.util.*;publicclassBoardClientextendsAppletimplementsMouseLis
3、tener{privateGraphicsgBuf;privateImageimgBuf;privateArrayListshapeList=newArrayList();publicstaticvoidmain(String[]args){Frameapp=newFrame("白板客户端");app.setSize(400,400);app.setLocationByPlatform(true);BoardClientapplet=newBoardClient();applet.addMouseListener(applet);app.add(app
4、let);app.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEventevent){event.getWindow().dispose();System.exit(0);}});app.show();applet.start();}publicvoidinit(){}publicvoidpaint(Graphicsg){intgraphWidth=bounds().width;intgraphHeight=bounds().height;imgBuf=creat
5、eImage(graphWidth,graphHeight);gBuf=imgBuf.getGraphics();gBuf.clearRect(0,0,graphWidth,graphHeight);Iteratorit=shapeList.iterator();Shapeshape;while(it.hasNext()){shape=(Shape)it.next();if(shape.type==Shape.CIRCLE)gBuf.drawOval(shape.x-10,shape.y-10,20,20);elseif(shape.type==Sha
6、pe.SQUARE)gBuf.drawRect(shape.x-10,shape.y-10,20,20);}g.drawImage(imgBuf,0,0,this);}publicvoidupdate(Graphicsg){paint(g);}publicvoidmousePressed(MouseEvente){Shapeshape;if(e.getButton()==e.BUTTON1){shape=newShape(Shape.CIRCLE,e.getPoint().x,e.getPoint().y);shapeList.add(shape);}
7、elseif(e.getButton()==e.BUTTON3){shape=newShape(Shape.SQUARE,e.getPoint().x,e.getPoint().y);shapeList.add(shape);}repaint();}/*Emptymethoddefinition.*/publicvoidmouseReleased(MouseEvente){}/*Emptymethoddefinition.*/publicvoidmouseEntered(MouseEvente){}/*Emptymethoddefinition.*/p
8、ublicvoidmouseExited(MouseEvente){}publicvoidmo