版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
JavaSocket编程实验目的:Socket编程是信息化时代必须掌握的一项技术。这次做这个Socket程序,只是简单的做了一下,了解了大致的流程。熟练掌握socket编程中的Socket类的常用方法,熟练掌握socket编程中流的使用方法实验原理基于客户机/服务器的Socket通信模型:首先在服务器端建一个ServerSocket,ServerSocketserverSocket=newServerSocket(port);其中port是你自行选择要监听的端口(最好是在1023以上);然后在客户端建一个Socket,Socketsocket=newSocket(host,port);其中host是你要连接的服务器的IP地址,port是与服务器的监听的port相同。这样就在两方建立了一个连接。然后用一系列的输入输出流来处理通话过程即可。要想实现p2p,实际上就相当于使一个程序既是服务器又是客户端。无需两种不同的程序。当然这其中还会涉及到一点线程的问题。三、实验步骤:本实验采用的是Java语言,首先查阅了Java的Socket编程的相关资料;熟悉了解了大致的过程。开始时设计了一个一个简单的服务器/客户端程序,在命令行下运行,服务器端只是简单的自动回复,而不能做一个人工互动。服务器和客户端是独立的两个不同的程序,服务器启动界面:客户端启动并发送消息后的界面:3、在原有的基础上改进成了一个多线程的聊天下棋类的程序,设计了简单的界面,由于没有用到NAT穿透,所以只能在局域网内进行简单的交互,在本机上程序运行界面:服务器端:客户端:实验中遇到的问题本来只想用一个socket来传递聊天信息和下棋的坐标,但是实现起来过于复杂,而且在涉及到悔棋时判断起来很麻烦,因此就用了2个线程来建立2个socket分别传递聊天和坐标,实现更加方便。程序要在监听与主线程之间轮流地切换,相当于在用户与界面交互时,要保持监听过程地同时运行,这样才能实时地捕获输入流并显示出来。实验总结简单的Socket编程,实现起来不复杂,但是实际上socket经常与线程,输入输出流相结合,要考虑很多种情况,如何建立连接,终止连接,异常处理,断线处理等等附:程序中的主要函数和类//多线程的run方法,两个线程分别处理聊天信息和坐标 publicvoidrun() { try{ serverchart=newServerSocket(6312); youchart=serverchart.accept(); }catch(IOExceptione2){ //TODOAuto-generatedcatchblock e2.printStackTrace(); } while(true) { if(Thread.currentThread().getName().equals("s")) { System.out.println("进入s线程++run!!"); try { serverchart=newServerSocket(6312); } catch(IOExceptione1) { System.out.println("Error:"+e1); } try { inchart=newDataInputStream(youchart.getInputStream()); outchart=newDataOutputStream(youchart.getOutputStream()); while(true) { Stringreadin=inchart.readUTF(); inMessage.append("服务器:"+inchart.readUTF());//读入信息 inMessage.append("\n"); System.out.println("进入sssssssssssssssss线程++run!!"); } } catch(Exceptione) { } } elseif(Thread.currentThread().getName().equals("t")) { System.out.println("进入t线程++run!!"); try { server=newServerSocket(6311); } catch(IOExceptione1) { System.out.println("Error:"+e1); } try { you=server.accept(); in=newDataInputStream(you.getInputStream()); out=newDataOutputStream(you.getOutputStream()); while(true) { Stringreadin=in.readUTF(); String[]socpoint=readin.split(","); if(Integer.parseInt(socpoint[0])==1024) { System.out.println("jjjjjj"+Integer.parseInt(socpoint[0])); points.remove(points.size()-1); repaint(); } else { addPoint(newPoint(Integer.parseInt(socpoint[0]), Integer.parseInt(socpoint[1]),Boolean .valueOf(socpoint[2]) .booleanValue())); System.out.println("--=="+getPoint(points)+"\n"); repaint(); System.out.println("进入tttttttttttt线程++run!!"); } } } catch(Exceptione) { } } } }//canvas的paint()方法,用来更新画布,绘制棋盘和棋子 publicvoidpaint(Graphicsg){ intj=0; for(j=0;j<11;j++) { g.drawLine(10+(500/10)*j,10,10+(500/10)*j,508); g.drawLine(10,10+(500/10)*j,508,10+(500/10)*j); } Iterator<Point>i=points.iterator(); while(i.hasNext()){ Pointp=(Point)i.next(); if(p.chess==true) { g.setColor(Color.black); g.fillOval(p.x-24,p.y-24,48,48); } else { g.setColor(Color.white); g.fillOval(p.x-24,p.y-24,48,48); } } }//鼠标点击的方法,用来记录棋子坐标,和调用paint()方法绘制棋子 publicvoidmouseClicked(MouseEvente){ if(btn==e.getSource()){ outMessage.setText("移除("+getPoint(points)+")的点"); points.remove(points.size()-1); try{ out.writeUTF("1024,1024,true"); }catch(IOExceptione2){ e2.printStackTrace(); }//写入信息 try{ FileWriterout=newFileWriter(file); Iterator<Point>i=points.iterator(); while(i.hasNext()){ Pointp=(Point)i.next(); x=p.x; y=p.y; Strings=x+","+y; out.write(s); out.write("\r\n"); } out.close(); FileReaderin=newFileReader(file); System.out.println("以行为单位读取文件内容,一次读一整行:"); BufferedReaderreader=newBufferedReader(newFileReader(file)); StringtempString=null; intline=1; //一次读入一行,直到读入null为文件结束 while((tempString=reader.readLine())!=null){ //显示行号 System.out.println("读取line"+line+":"+tempString); String[]s=tempString.split(","); addReadpoint(newPoint(Integer.parseInt(s[0]), Integer.parseInt(s[1]),true)); line++; } intnum=0; Iterator<Point>j=Readpoint.iterator(); while(j.hasNext()){ num++; Pointp=(Point)j.next(); x=p.x; y=p.y; Strings="Readpoint中第"+num+"个"+x+","+y; System.out.println(s); } reader.close(); Readpoint.removeAll(Readpoint); }catch(IOExceptione2){ System.out.println(e2); } repaint(); } }源码:canvas.javapackagesocket;importjavax.swing.*;importjava.awt.event.*;importjava.awt.geom.Rectangle2D;importjava.awt.image.BufferedImage;importjava.awt.*;importjava.io.*;import.*;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.Vector;/*Client*/classPoint{ intx,y;booleanchess;//chess=trueblack//chess=falsewhite; Point(){ } Point(intx,inty,booleanchess){ this.x=x; this.y=y; this.chess=chess; }}classmapDOT{ intx,y;}publicclasscanvasextendsCanvasimplementsMouseListener,Runnable,ActionListener{ /** * */ privatestaticfinallongserialVersionUID=855858279582070325L; ArrayList<Point>points=null; ArrayList<Point>Readpoint=null; Strings=null,s1=null; Socketmysocket=null,mysocketchart=null; DataInputStreamin=null,inchart=null; DataOutputStreamout=null,outchart=null; //canvasServerth=newcanvasServer(); Threadthread=newThread(this,"t"); Threadtalk=newThread(this,"s"); mapDOTcoin=newmapDOT(); canvasf; JButtonbtn,connect; JTextAreainMessage=newJTextArea(34,10); JLabelmessageBox=newJLabel("消息框:"); JLabelinfor=newJLabel("info:"); JLabeladdressIP=newJLabel("地址:"); JTextFieldIP=newJTextField("",16); JButtonb=newJButton("发送"); JButtonconnectServer=newJButton("连接"); JTextFieldoutMessage; Framewindow; PanelpCenter,pEast; Rectangle2Drect; BufferedImageimage; Graphics2Dggg; intx,y,n; booleanchess; Vectorv=null; Pointp1; Filefile=newFile("chess.txt"); publiccanvas(){ points=newArrayList<Point>(); Readpoint=newArrayList<Point>(); v=newVector(); btn=newJButton("取消"); connect=newJButton("连接"); outMessage=newJTextField(16); pEast=newPanel(); pEast.add(btn); pEast.add(addressIP); pEast.add(IP); pEast.add(connectServer); pEast.add(infor); pEast.add(outMessage); //pEast.add(messageBox); pEast.add(b); b.addActionListener(this); connectServer.addActionListener(newActionListener() { publicvoidactionPerformed(ActionEvente) { s1=IP.getText(); thread.start(); talk.start(); System.out.println("kkkkkkkkkkk"+thread); System.out.println("sssssssss"+talk); } }); connect.addMouseListener(this); btn.addMouseListener(this); this.addMouseListener(this); image=newBufferedImage(200,200,BufferedImage.TYPE_INT_RGB); ggg=image.createGraphics(); rect=newRectangle2D.Double(0,0,200,200); ggg.setColor(getBackground()); ggg.fill(rect); window=newFrame("Client"); pCenter=newPanel(); pCenter.setLayout(null); pCenter.add(this); //pCenter pCenter.setBackground(Color.gray); pCenter.setLocation(0,40); this.setBounds(0,40,520,600); this.setBackground(Color.LIGHT_GRAY); FlowLayoutflow=newFlowLayout(); flow.setAlignment(FlowLayout.TRAILING); flow.setHgap(2); flow.setVgap(3); window.setLayout(flow); //window.setLayout(null); window.add(pEast); window.add(this); window.add(newJScrollPane(inMessage)); //window.add(pCenter); //window.add(pEast); window.setVisible(true); window.addWindowListener(newWindowAdapter(){ publicvoidwindowClosing(WindowEvente){ System.exit(0); } }); window.setBounds(280,90,658,620); window.validate(); } publicvoidactionPerformed(ActionEventevent) { try { outchart.writeUTF(outMessage.getText());//写入信息 //inMessage.append("你说:"+outMessage.getText()+"\n"); //outMessage.setText(""); } catch(Exceptione) { } try { outchart.writeUTF(outMessage.getText());//写入信息 inMessage.append("你说:"+outMessage.getText()+"\n"); outMessage.setText(""); } catch(Exceptione) { } } publicvoidrun() { while(true) { if(Thread.currentThread().getName().equals("t")) { System.out.println("进入线程trun!!"); try { mysocket=newSocket(s1,6311); //mysocket=newSocket("localhost",6311); inMessage.append("连接成功!~~~~"+"\n"); in=newDataInputStream(mysocket.getInputStream()); out=newDataOutputStream(mysocket.getOutputStream()); while(true) { Stringreadin=in.readUTF();// // if(readin.indexOf(",")==-1)// {// inMessage.append("服务器:"+in.readUTF());//读入信息// inMessage.append("\n");// }// //else { String[]socpoint=readin.split(","); if(Integer.parseInt(socpoint[0])==1024){ System.out.println("jjjjjj" +Integer.parseInt(socpoint[0])); points.remove(points.size()-1); repaint(); } else { addPoint(newPoint( Integer.parseInt(socpoint[0]), Integer.parseInt(socpoint[1]),Boolean .valueOf(socpoint[2]) .booleanValue())); System.out.println("--=="+getPoint(points) +"\n"); repaint(); } }// Stringreadin=in.readUTF();// // String[]socpoint=readin.split(",");// if(socpoint.length>2)// {// addPoint(newPoint(Integer.parseInt(socpoint[0]),// Integer.parseInt(socpoint[1]),Boolean.valueOf(socpoint[2]).booleanValue()));// System.out.println("--=="+getPoint(points)+"\n");// repaint();// }// else// {// inMessage.append("服务器:"+in.readUTF());//读入信息// inMessage.append("\n");// }// try// {// Thread.sleep(500);// }// catch(InterruptedExceptione)// {// //TODOAuto-generatedcatchblock// e.printStackTrace();// } } } catch(IOExceptione) { System.out.println("无法连接!"); } } elseif(Thread.currentThread().getName().equals("s")) { System.out.println("进入s线程++run!!"); try { mysocketchart=newSocket(s1,6312); //mysocket=newSocket("localhost",6311); inMessage.append("聊天连接成功!~~~~"+"\n"); inchart=newDataInputStream(mysocketchart.getInputStream()); outchart=newDataOutputStream(mysocketchart.getOutputStream()); while(true) { Stringreadin=inchart.readUTF();// // if(readin.indexOf(",")==-1)// { inMessage.append("服务器:"+inchart.readUTF());//读入信息 inMessage.append("\n");// }// //else // Stringreadin=in.readUTF();// // String[]socpoint=readin.split(",");// if(socpoint.length>2)// {// addPoint(newPoint(Integer.parseInt(socpoint[0]),// Integer.parseInt(socpoint[1]),Boolean.valueOf(socpoint[2]).booleanValue()));// System.out.println("--=="+getPoint(points)+"\n");// repaint();// }// else// {// inMessage.append("服务器:"+in.readUTF());//读入信息// inMessage.append("\n");// }// try// {// Thread.sleep(500);// }// catch(InterruptedExceptione)// {// //TODOAuto-generatedcatchblock// e.printStackTrace();// } } } catch(IOExceptione) { System.out.println("无法连接++聊天!"); } } } } publicvoidpaint(Graphicsg){ //super.paint(g); //g.drawOval(x-10,y-10,x+10,y+10); //g.drawLine(10,10,10,500); //g.drawLine(10,10,500,10); intj=0; for(j=0;j<11;j++) { g.drawLine(10+(500/10)*j,10,10+(500/10)*j,508); g.drawLine(10,10+(500/10)*j,508,10+(500/10)*j); } Iterator<Point>i=points.iterator(); while(i.hasNext()){ Pointp=(Point)i.next(); if(p.chess==true) { g.setColor(Color.black); g.fillOval(p.x-24,p.y-24,48,48); } else { g.setColor(Color.white); g.fillOval(p.x-24,p.y-24,48,48); } } } publicvoidaddPoint(Pointp){ points.add(p); } publicvoidaddReadpoint(Pointp){ Readpoint.add(p); } publicvoiddelReadpoint(Pointp){ Readpoint.removeAll(Readpoint); } publicStringgetPoint(ArrayList<Point>points){ x=points.get(points.size()-1).x; y=points.get(points.size()-1).y; chess=points.get(points.size()-1).chess; Strings=x+","+y+","+chess; returns; } publicvoidgetmap(intx,inty) { inti=0; //intj=0; for(i=0;i<6;i++) { if(10+25*i-12.5<x) coin.x=i; } for(i=0;i<11;i++) { if(10+25*i-12.5<x) coin.y=i; } System.out.println("coin中的值为"+"cion.x="+coin.x+"coin.y="+coin.y); coin.x=10+25*coin.x-12; coin.y=10+25*coin.y-12; System.out.println("在此点画圆~~"+"cion.x="+coin.x+"coin.y="+coin.y); } /* *publicStringPoint(ArrayList<Point>points){ * *return"x::"+points+",y="+y;} */ @Override publicvoidmouseClicked(MouseEvente){ //Iterator<Point>i=points.iterator(); if(btn==e.getSource()){ outMessage.setText("移除("+getPoint(points)+")的点"); points.remove(points.size()-1); try{ out.writeUTF("1024,1024,true"); }catch(IOExceptione2){ //TODOAuto-generatedcatchblock e2.printStackTrace(); }//写入信息// try{ FileWriterout=newFileWriter(file); Iterator<Point>i=points.iterator(); while(i.hasNext()){ Pointp=(Point)i.next(); x=p.x; y=p.y; Strings=x+","+y; out.write(s); out.write("\r\n"); } out.close(); //ArrayList<Point>Readpoint=null; FileReaderin=newFileReader(file); System.out.println("以行为单位读取文件内容,一次读一整行:"); BufferedReaderreader=newBufferedReader(newFileReader(file)); StringtempString=null; intline=1; //一次读入一行,直到读入null为文件结束 while((tempString=reader.readLine())!=null){ //显示行号 System.out.println("读取line"+line+":"+tempString); String[]s=tempString.split(","); addReadpoint(newPoint(Integer.parseInt(s[0]), Integer.parseInt(s[1]),false)); line++; } intnum=0; Iterator<Point>j=Readpoint.iterator(); while(j.hasNext()){ num++; Pointp=(Point)j.next(); x=p.x; y=p.y; Strings="Readpoint中第"+num+"个"+x+","+y; System.out.println(s); } reader.close(); Readpoint.removeAll(Readpoint); }catch(IOExceptione2){ System.out.println(e2); } repaint(); } } @Override publicvoidmousePressed(MouseEvente){ //TODOAuto-generatedmethodstub f=(canvas)(e.getSource()); // getmap(e.getX(),e.getY()); //f.addPoint(newPoint(coin.x,coin.y)); intflag=0; inti=0; intx1=0; inty1=0; //intj=0; for(i=0;i<12;i++) { if((10+50*i-25)>e.getX()) { if(e.getX()>(10+50*(i-1)-25)) x1=i; } } for(i=0;i<11;i++) { if((10+50*i-25)>e.getY()) { if(e.getY()>(10+50*(i-1)-25)) y1=i; } } System.out.println("coin中的值为"+"cion.x="+x1+"coin.y="+y1); x1=10+50*x1-50; y1=10+50*y1-50; //f.addPoint(newPoint(x1,y1,false)); try{ FileReaderin=newFileReader(file); System.out.println("以行为单位读取文件内容,一次读一整行:"); BufferedReaderreader=newBufferedReader(newFileReader(file)); StringtempString=null; intline=1; //一次读入一行,直到读入null为文件结束 while((tempString=reader.readLine())!=null){ //显示行号 System.out.println("line"+line+":"+tempString); line++; String[]socpoint=tempString.split(","); if(socpoint[0].equals(String.valueOf(x1))&&socpoint[1].equals(String.valueOf(y1))) { flag++; JOptionPane.showMessageDialog(null,"请不要重复下子!!","提示",JOptionPane.ERROR_MESSAGE); } } reader.close(); }catch(FileNotFoundExceptione3){ //TODOAuto-generatedcatchblock e3.printStackTrace(); }catch(IOExceptione2){ //TODOAuto-generatedcatchblock e2.printStackTrace(); } if(flag==0) { f.addPoint(newPoint(x1,y1,false)); } try{ out.writeUTF(getPoint(points)); }catch(IOExceptione2){ //TODOAuto-generatedcatchblock e2.printStackTrace(); }//写入信息// inMessage.append("你说:"+getPoint(points)+"\n");// String[]socpoint=getPoint(points).split(",");// addPoint(newPoint(Integer.parseInt(socpoint[0]),// Integer.parseInt(socpoint[1]),Boolean.valueOf(socpoint[2]).booleanValue()));// // repaint(); //f.addPoint(newPoint(e.getX(),e.getY())); x=e.getX(); y=e.getY(); outMessage.setText("鼠标点击("+x+","+y+")"); try{ FileWriterout=newFileWriter(file); Iterator<Point>j=points.iterator(); while(j.hasNext()){ Pointp=(Point)j.next(); x=p.x; y=p.y; Strings=x+","+y; out.write(s); out.write("\r\n"); } out.close(); FileReaderin=newFileReader(file); System.out.println("以行为单位读取文件内容,一次读一整行:"); BufferedReaderreader=newBufferedReader(newFileReader(file)); StringtempString=null; intline=1; //一次读入一行,直到读入null为文件结束 while((tempString=reader.readLine())!=null){ //显示行号 System.out.println("line"+line+":"+tempString); line++; } reader.close(); }catch(IOExceptione1){ System.out.println(e1); } repaint(); } @Override publicvoidmouseReleased(MouseEvente){ //TODOAuto-generatedmethodstub } @Override publicvoidmouseEntered(MouseEvente){ //TODOAuto-generatedmethodstub } @Override publicvoidmouseExited(MouseEvente){ //TODOAuto-generatedmethodstub } publicstaticvoidmain(Stringargs[]){ newcanvas(); }}canvasServer.javapackagesocket;importjavax.swing.*;importjava.awt.event.*;importjava.awt.geom.Rectangle2D;importjava.awt.image.BufferedImage;importjava.awt.*;importjava.io.*;import.*;importjava.util.ArrayList;importjava.util.Iterator;importjava.util.Vector;/*Server*/publicclasscanvasServerextendsCanvasimplementsMouseListener,Runnable,ActionListener{ /** * */ privatestaticfinallongserialVersionUID=855858279582070325L; ArrayList<Point>points=null; ArrayList<Point>Readpoint=null; ServerSocketserver=null,serverchart=null; Socketyou,youchart; DataOutputStreamout=null,outchart=null; DataInputStreamin=null,inchart=null; Threadthread=newThread(this,"t"); Threadtalk=newThread(this,"s"); Strings2; mapDOTcoin=newmapDOT(); canvasServerf; JButtonbtn,connect; JTextAreainMessage=newJTextArea(34,10); JLabelmessageBox=newJLabel("消息框:"); JLabelinfor=newJLabel("info:"); JButtonb=newJButton("发送"); JTextFieldoutMessage; Framewindow; PanelpCenter,pEast; Rectangle2Drect; BufferedImageimage; Graphics2Dggg; intx,y,n; booleanchess; Vectorv=null; Pointp1; Filefile=newFile("chess.txt"); publiccanvasServer(){ points=newArrayList<Point>(); Readpoint=newArrayList<Point>(); v=newVector(); btn=newJButton("取消"); outMessage=newJTextField(19); pEast=newPanel(); pEast.add(btn); pEast.add(infor); pEast.add(outMessage); //pEast.add(messageBox); pEast.add(b); b.addActionListener(this); btn.addMouseListener(this); this.addMouseListener(this); setBackground(Color.white); setBackground(Color.white); image=newBufferedImage(200,200,BufferedImage.TYPE_INT_RGB); ggg=image.createGraphics(); rect=newRectangle2D.Double(0,0,200,200); ggg.setColor(getBackground()); ggg.fill(rect); window=newFrame("Server"); pCenter=newPanel(); pCenter.setLayout(null); pCenter.add(this); //pCenter pCenter.setBackground(Color.gray); pCenter.setLocation(0,40); this.setBounds(0,40,520,600); this.setBackground(Color.LIGHT_GRAY); FlowLayoutflow=newFlowLayout(); flow.setAlignment(FlowLayout.TRAILING); flow.setHgap(2); flow.setVgap(3); window.setLayout(flow); //window.setLayout(null); window.add(pEast); window.add(this); window.add(newJScrollPane(inMessage)); //window.add(pCenter); //window.add(pEast); window.setVisible(true); window.addWindowListener(newWindowAdapter(){ publicvoidwindowClosing(WindowEvente){ System.exit(0); } }); window.setBounds(280,90,658,620); window.validate(); thread.start(); talk.start(); System.out.println("kkkkkkkkkkk"+thread); System.out.println("sssssssss"+talk); } publicvoidactionPerformed(ActionEventevent) { try { outchart.writeUTF(outMessage.getText());//写入信息 //inMessage.append("你说:"+outMessage.getText()+"\n"); //outMessage.setText(""); } catch(Exceptione) { } try { outchart.writeUTF(outMessage.getText());//写入信息 inMessage.append("你说:"+outMessage.getText()+"\n"); outMessage.setText(""); } catch(Exceptione) { } } publicvoidrun() { try{ serverchart=newServerSocket(6312); youchart=serverchart.accept(); }catch(IOExceptione2){ //TODOAuto-generatedcatchblock e2.printStackTrace(); } while(true) { if(Thread.currentThread().getName().equals("s")) { System.out.println("进入s线程++run!!"); try { serverchart=newServerSocket(6312); } catch(IOExceptione1) { System.out.println("Error:"+e1); } try { inchart=newDataInputStream(youchart.getInputStream()); outchart=newDataOutputStream(youchart.getOutputStream()); while(true) { Stringreadin=inchart.readUTF(); inMessage.append("服务器:"+inchart.readUTF());//读入信息 inMessage.append("\n"); System.out.println("进入sssssssssssssssss线程++run!!"); } } catch(Exceptione) { } } elseif(Thread.currentThread().getName().equals("t")) { System.out.println("进入t线程++run!!"); try { server=newServerSocket(6311); } catch(IOExceptione1) { System.out.println("Error:"+e1); } try { you=server.accept(); in=newDataInputStream(you.getInputStream()); out=newDataOutputStream(you.getOutputStream()); while(true) { Stringreadin=in.readUTF(); String[]socpoint=readin.split(","); if(Integer.parseInt(socpoint[0])==1024) { System.out.println("jjjjjj"+Integer.parseInt(socpoint[0])); points.remove(points.size()-1); repaint(); } else { addPoint(newPoint(Integer.parseInt(socpoint[0]), Integer.parseInt(socpoint[1]),Boolean .valueOf(socpoint[2]) .booleanValue())); System.out.println("--=="+getPoint(points)+"\n"); repaint(); System.out.println("进入tttttttttttt线程++run!!"); } } } catch(Exceptione) { } } } } publicvoidpaint(Graphicsg){ intj=0; for(j=0;j<11;j++) { g.drawLine(10+(500/10)*j,10,10+(500/10)*j,508); g.drawLine(10,10+(500/10)*j,508,10+(500/10)*j); } Iterator<Point>i=points.iterator(); while(i.hasNext()){ Pointp=(Point)i.next(); if(p.chess==true) { g.setColor(Color.black); g.fillOval(p.x-24,p.y-24,48,48); } else { g.setColor(Color.white); g.fillOval(p.x-24,p.y-24,48,48); } } } publicvoidaddPoint(Pointp){ points.add(p); } publicvoidaddReadpoint(Pointp){ Readpoint.add(p); } publicvoiddelReadpoint(Pointp){ Readpoint.removeAll(Readpoint); } publicStringgetPoint(ArrayList<Point>points){ x=points.get(points.size()-1).x; y=points.get(points.size()-1).y; chess=points.get(points.size()-1).chess; Strings=x+","+y+","+chess; returns; } publicvoidgetmap(intx,inty) { inti=0; //intj=0; for(i=0;i<6;i++) { if(10+25*i-12.5<x) coin.x=i; } for(i=0;i<11;i++) { if(10+25*i-12.5<x) coin.y=i; } System.out.println("coin中的值为"+"cion.x="+coin.x+"coin.y="+coin.y); coin.x=10+25*coin.x-12; coin.y=10+25*coin.y-12; System.out.println("在此点画圆~~"+"cion.x="+coin.x+"coin.y="+coin.y); } /* *publicStringPoint(ArrayList<Point>points){ * *return"x::"+points+",y="+y;} */ @Override publicvoidmouseClicked(MouseEvente){ if(btn==e.getSource()){ outMessage.setText("移除("+getPoint(points)+")的点"); points.remove(points.size()-1); try{ out.writeUTF("1024,1024,true"); }catch(IOExceptione2){ e2.printStackTrace(); }//写入信息 try{ FileWriterout=newFileWriter(file); Iterator<Point>i=points.iterator(); while(i.hasNext()){ Pointp=(Point)i.next(); x=p.x; y=p.y; Strings=x+","+y; out.write(s); out.write("\r\n"); } out.close(); FileReaderin=newFileReader(file); System.out.println("以行为单位读取文件内容,一次读一整行:"); BufferedReaderreader=newBufferedReader(newFileReader(file)); StringtempString=null; intline=1; //一次读入一行,直到读入null为文件结束 while((tempString=reader.readLine())!=null){ //显示行号 System.out.println("读取line"+line+":"+tempString); String[]s=tempString.split(","); addReadpoint(newPoint(Integer.parseInt(s[0]), Integer.parseInt(s[1]),true)); line++; } intnum=0; Iterator<Point>j=Readpoint.iterator(); while(j.hasNext()){ num++; Pointp=(Point)j.next(); x=p.x; y=p.y; Strings="Readpoint中第"+num+"个"+x+","+y; System.out.println(s); } reader.close(); Readpoint.removeAll(Readpoint); }catch(IOExceptione2){ System.out.println(e2); } repaint(); } } @Override publicvoidmousePressed(MouseEvente){ //TODOAuto-generatedmethodstub f=(canvasServer)(e.getSource()); // getmap(e.getX(),e.getY()); //f.addPoint(newPoint(coin.x,coin.y)); intflag=0; inti=0; intx1=0; inty1=0; //intj=0; for(i=0;i<12;i++) { if((10+50*i-25)>e.getX()) { if(e.getX()>(10+50*(i-1)-25)) x1=i; } } for(i=0;i<11;i++) { if((10+50*i-25)>e.getY()) { if(e.getY()>(10+50*(i-1)-25)) y1=i; } } System.out.println("coin中的值为"+"cion.x="+x1+"coin.y="+y1); x1=10+50*x1-50; y1=10+50*y1-50; //JOptionPane.showMessageDialog(null,"请不要!!","提示",JOptionPane.ERROR_MESSAGE); try{ FileReaderin=newFileReader(file); System.out.println("以行为单位读取文件内容,一次读一整行:"); BufferedReaderreader=newBufferedReader(newFileReader(file)); StringtempString=null; intline=1; //一次读入一行,直到读入null为文件结束 while((tempString=reader.readLine())!=null){ //显示行号 System.out.println("line"+line+":"+tempString); line++; String[]socpoint=tempString.split(","); if(Integer.parseInt(socpoint[0])==x1&&Integer.parseInt(socpoint[1])==y1) { flag++; JOptionPane.showMessageDialog(null,"请不要重复下子!!","提示",JOptionPane.ERROR_MESSAGE); } } reader.close(); }catch(FileNotFoundExceptione3){ //TODOAuto-generatedcatchblock e3.printStackTrace(); }catch(IOExceptione2){ //TODOAuto-generatedcatchblo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 农村宅基地房屋租赁合同(含农村电商培训2025年度版)
- 2025年度建筑工程质量鉴定合同3篇
- 2024年中国湿毒清片市场调查研究报告
- 二零二五年度农村自建房改造升级工程承包合同
- 2024年中国气动控制夹具市场调查研究报告
- 2025年度消防工程设计变更及改造合同3篇
- 《后张法预应力混凝土双T板设计应用研究》
- 2024年双色CD丝印机项目可行性研究报告
- 2024年卧室衣橱项目可行性研究报告
- 2024年中国女式短袖翻领羊毛衫市场调查研究报告
- 中小企业内部控制与风险管理(第二版)项目一:内部控制与风险管理基础
- 驾驶舱资源管理绪论课件
- 声艺 EPM8操作手册
- 西北农林科技大学专业学位研究生课程案例库建设项目申请书(MBA)
- 外墙保温、真石漆施工技术交底
- 车床日常点检表
- 配网工程施工监理管理要点~.docx
- 国内No.7信令方式技术规范----综合业务数字网用户部分(ISUP)
- 尾矿库在线监测方案)
- 房屋安全简易鉴定表.docx
- FSSC运营管理制度(培训管理办法)
评论
0/150
提交评论