问题描述
跟着马士兵视频学得坦克大战网络版,找不到问题在哪求高手帮助两个Client端链接到Server端后,打死一辆坦克在另一个client端却不消失//classTankServerimportjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.IOException;importjava.net.DatagramPacket;importjava.net.DatagramSocket;importjava.net.InetSocketAddress;importjava.net.ServerSocket;importjava.net.Socket;importjava.net.SocketException;importjava.util.ArrayList;importjava.util.List;publicclassTankServer{privatestaticintID=100;publicstaticfinalintTCP_PORT=8888;publicstaticfinalintUDP_PORT=7777;List<Client>clients=newArrayList<Client>();publicvoidstart(){newThread(newUDPThread()).start();ServerSocketss=null;try{ss=newServerSocket(TCP_PORT);}catch(IOExceptione){e.printStackTrace();}while(true){Sockets=null;try{s=ss.accept();DataInputStreamdis=newDataInputStream(s.getInputStream());StringIP=s.getInetAddress().getHostAddress();intudpPort=dis.readInt();Clientc=newClient(IP,udpPort);//在main方法里这步会出错,所以要重建一个动态的方法clients.add(c);DataOutputStreamdos=newDataOutputStream(s.getOutputStream());dos.writeInt(ID++);//s.close();System.out.println("AClientConnectAddr="+s.getInetAddress()+"i"+s.getPort()+"___UDPPORT:"+udpPort);}catch(IOExceptione){e.printStackTrace();}finally{if(s!=null){try{s.close();s=null;}catch(IOExceptione){e.printStackTrace();}}}}}publicstaticvoidmain(String[]args){newTankServer().start();}privateclassClient{StringIP;intudpPort;publicClient(StringIP,intudpPort){this.IP=IP;this.udpPort=udpPort;}}privateclassUDPThreadimplementsRunnable{byte[]buf=newbyte[1024];publicvoidrun(){DatagramSocketds=null;try{ds=newDatagramSocket(UDP_PORT);}catch(SocketExceptione){e.printStackTrace();}System.out.println("UDPthreadstartedatport!"+UDP_PORT);while(ds!=null){DatagramPacketdp=newDatagramPacket(buf,buf.length);try{ds.receive(dp);for(inti=0;i<clients.size();i++){Clientc=clients.get(i);dp.setSocketAddress(newInetSocketAddress(c.IP,c.udpPort));ds.send(dp);}System.out.println("apackagereceive");}catch(IOExceptione){e.printStackTrace();}}}}}//classTankClientimportjava.awt.Button;importjava.awt.Color;importjava.awt.Dialog;importjava.awt.FlowLayout;importjava.awt.Frame;importjava.awt.Graphics;importjava.awt.Image;importjava.awt.Label;importjava.awt.TextField;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.util.List;importjava.util.ArrayList;publicclassTankClientextendsFrame{publicstaticfinalintGAME_WIDTH=800;publicstaticfinalintGAME_HEIGHT=600;ImageoffScreenImage=null;TankmyTank=newTank(50,50,true,Direction.STOP,this);List<Explode>explodes=newArrayList<Explode>();List<Missile>missile=newArrayList<Missile>();List<Tank>tanks=newArrayList<Tank>();NetClientnc=newNetClient(this);ConaDialogdialog=newConaDialog();publicvoidpaint(Graphicsg){g.drawString("多少发炮弹"+missile.size(),10,50);g.drawString("爆炸了多少次"+explodes.size(),10,70);for(inti=0;i<missile.size();i++){Missilem=missile.get(i);//m.hitTanks(tanks);if(m.hitTank(myTank)){TankDeadMsgmsg=newTankDeadMsg(myTank.id);nc.send(msg);MissileDeadMsgmdmMsg=newMissileDeadMsg(m.tankId,m.id);nc.send(mdmMsg);}m.draw(g);}for(inti=0;i<explodes.size();i++){Explodee=explodes.get(i);e.draw(g);}for(inti=0;i<tanks.size();i++){Tankt=tanks.get(i);t.draw(g);}myTank.draw(g);}publicvoidupdate(Graphicsg){if(offScreenImage==null){offScreenImage=this.createImage(GAME_WIDTH,GAME_HEIGHT);}GraphicsgOffScreen=offScreenImage.getGraphics();Colorc=gOffScreen.getColor();gOffScreen.setColor(Color.GREEN);gOffScreen.fillRect(0,0,GAME_WIDTH,GAME_HEIGHT);gOffScreen.setColor(c);paint(gOffScreen);g.drawImage(offScreenImage,0,0,null);}publicvoidlauchFrame(){/*for(inti=0;i<10;i++){tanks.add(newTank(50+40*(i+1),50,false,Direction.D,this));}*/this.setLocation(300,200);this.setSize(GAME_WIDTH,GAME_HEIGHT);this.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});this.setTitle("TankWar");this.setResizable(false);this.setBackground(Color.green);this.addKeyListener(newKeyMoniter());this.setVisible(true);newThread(newPaintThread()).start();}publicstaticvoidmain(String[]args){TankClienttc=newTankClient();tc.lauchFrame();}publicclassPaintThreadimplementsRunnable{publicvoidrun(){while(true){repaint();try{Thread.sleep(20);}catch(InterruptedExceptione){e.printStackTrace();}}}}classKeyMoniterextendsKeyAdapter{publicvoidkeyReleased(KeyEvente){myTank.keyReleased(e);}publicvoidkeyPressed(KeyEvente){intkey=e.getKeyCode();if(key==KeyEvent.VK_C){dialog.setVisible(true);}else{myTank.keyPressed(e);}}}classConaDialogextendsDialog{Buttonb=newButton("确定");TextFieldtfip=newTextField("127.0.0.1",12);TextFieldtfPort=newTextField(""+TankServer.TCP_PORT,4);TextFieldtfMyUDPPort=newTextField("2223",4);publicConaDialog(){super(TankClient.this,true);this.setLayout(newFlowLayout());this.add(newLabel("IP:"));this.add(tfip);this.add(newLabel("Port:"));this.add(tfPort);this.add(newLabel("MyUDPPort:"));this.add(tfMyUDPPort);this.add(b);this.setLocation(300,300);this.pack();this.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){setVisible(false);}});b.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){StringIP=tfip.getText().trim();intport=Integer.parseInt(tfPort.getText().trim());intmyUDPPort=Integer.parseInt(tfMyUDPPort.getText().trim());nc.setUdpPort(myUDPPort);nc.connect(IP,port);setVisible(false);}});}}}
解决方案
解决方案二:
//class?Tankimportjava.awt.Color;importjava.awt.Graphics;importjava.awt.Rectangle;importjava.awt.event.KeyEvent;importjava.util.Random;publicclassTank{intid;publicstaticfinalintXSPEED=5;publicstaticfinalintYSPEED=5;publicstaticfinalintWIDTH=30;publicstaticfinalintHEIGHT=30;privatebooleanlive=true;privatestaticRandomr=newRandom();//定义为静态的,大家伙都来用这一个TankClienttc;booleangood;intx,y;booleanBU,BD,BR,BL;Directiondir=Direction.STOP;DirectionptDir=Direction.D;privateintstep=r.nextInt(12)+3;publicTank(intx,inty,booleangood){this.x=x;this.y=y;this.good=good;}publicTank(intx,inty,booleangood,Directiondir,TankClienttc){this(x,y,good);this.dir=dir;this.tc=tc;}publicvoiddraw(Graphicsg){if(!live){if(!good){tc.tanks.remove(this);}}Colorc=g.getColor();if(good)g.setColor(Color.red);elseg.setColor(Color.BLUE);g.fillOval(x,y,WIDTH,HEIGHT);g.drawString("id:"+id,x,y-10);g.setColor(c);switch(ptDir){caseL:g.drawLine(x+Tank.WIDTH/2,y+Tank.HEIGHT/2,x,y+Tank.HEIGHT/2);break;caseLU:g.drawLine(x+Tank.WIDTH/2,y+Tank.HEIGHT/2,x,y);break;caseU:g.drawLine(x+Tank.WIDTH/2,y+Tank.HEIGHT/2,x+Tank.WIDTH/2,y);break;caseRU:g.drawLine(x+Tank.WIDTH/2,y+Tank.HEIGHT/2,x+Tank.WIDTH,y);break;caseR:g.drawLine(x+Tank.WIDTH/2,y+Tank.HEIGHT/2,x+Tank.WIDTH,y+Tank.HEIGHT/2);break;caseRD:g.drawLine(x+Tank.WIDTH/2,y+Tank.HEIGHT/2,x+Tank.WIDTH,y+Tank.HEIGHT);break;caseD:g.drawLine(x+Tank.WIDTH/2,y+Tank.HEIGHT/2,x+Tank.WIDTH/2,y+Tank.HEIGHT);break;caseLD:g.drawLine(x+Tank.WIDTH/2,y+Tank.HEIGHT/2,x,y+Tank.HEIGHT);break;}move();}voidmove(){switch(dir){caseL:x-=XSPEED;break;caseLU:x-=XSPEED;y-=YSPEED;break;caseU:y-=YSPEED;break;caseRU:x+=XSPEED;y-=YSPEED;break;caseR:x+=XSPEED;break;caseRD:x+=XSPEED;y+=YSPEED;break;caseD:y+=YSPEED;break;caseLD:x-=XSPEED;y+=YSPEED;break;caseSTOP:break;}if(dir!=Direction.STOP){ptDir=dir;}if(x<0)x=0;if(y<30)y=30;if(x+Tank.WIDTH>TankClient.GAME_WIDTH)x=TankClient.GAME_WIDTH-Tank.WIDTH;if(y+Tank.HEIGHT>TankClient.GAME_HEIGHT)y=TankClient.GAME_HEIGHT-Tank.HEIGHT;}publicvoidkeyPressed(KeyEvente){intkey=e.getKeyCode();switch(key){caseKeyEvent.VK_RIGHT:BR=true;break;caseKeyEvent.VK_LEFT:BL=true;break;caseKeyEvent.VK_UP:BU=true;break;caseKeyEvent.VK_DOWN:BD=true;break;}locateDirection();}voidlocateDirection(){DirectionoldDir=this.dir;if(BL&&!BU&&!BR&&!BD)dir=Direction.L;elseif(BL&&BU&&!BR&&!BD)dir=Direction.LU;elseif(!BL&&BU&&!BR&&!BD)dir=Direction.U;elseif(!BL&&BU&&BR&&!BD)dir=Direction.RU;elseif(!BL&&!BU&&BR&&!BD)dir=Direction.R;elseif(!BL&&!BU&&BR&&BD)dir=Direction.RD;elseif(!BL&&!BU&&!BR&&BD)dir=Direction.D;elseif(BL&&!BU&&!BR&&BD)dir=Direction.LD;elseif(!BL&&!BU&&!BR&&!BD)dir=Direction.STOP;if(dir!=oldDir){TankMoveMsgmsg=newTankMoveMsg(id,x,y,dir,ptDir);tc.nc.send(msg);}}publicvoidkeyReleased(KeyEvente){intkey=e.getKeyCode();switch(key){caseKeyEvent.VK_CONTROL:fire();break;caseKeyEvent.VK_LEFT:BL=false;break;caseKeyEvent.VK_RIGHT:BR=false;break;caseKeyEvent.VK_UP:BU=false;break;caseKeyEvent.VK_DOWN:BD=false;break;}locateDirection();}publicMissilefire(){if(!live)returnnull;intx=this.x+Tank.WIDTH/2-Missile.WIDTH/2;inty=this.y+Tank.HEIGHT/2-Missile.HEIGHT/2;Missilem=newMissile(id,x,y,this.good,this.ptDir,this.tc);tc.missile.add(m);MissileNewMsgmsg=newMissileNewMsg(m);tc.nc.send(msg);returnm;}publicRectanglegetRect(){;returnnewRectangle(x,y,WIDTH,HEIGHT);}publicbooleanisLive(){returnlive;}publicvoidsetLive(booleanlive){this.live=live;}}//classMissileimportjava.awt.Color;importjava.awt.Graphics;importjava.awt.Rectangle;importjava.util.List;publicclassMissile{publicstaticfinalintXSPEED=10;publicstaticfinalintYSPEED=10;publicstaticfinalintWIDTH=10;publicstaticfinalintHEIGHT=10;privatestaticintID=1;intx,y;Directiondir;privateTankClienttc;inttankId;intid;booleanlive=true;booleangood;publicMissile(inttankId,intx,inty,booleangood,Directiondir){this.tankId=tankId;this.x=x;this.y=y;this.good=good;this.dir=dir;this.id=ID++;}publicMissile(inttankId,intx,inty,booleangood,Directiondir,TankClienttc){this.tankId=tankId;this.x=x;this.y=y;this.good=good;this.dir=dir;this.tc=tc;}publicvoiddraw(Graphicsg){if(!live){tc.missile.remove(this);return;}Colorc=g.getColor();g.setColor(Color.BLACK);g.fillOval(x,y,WIDTH,HEIGHT);g.setColor(c);move();}voidmove(){switch(dir){caseL:x-=XSPEED;break;caseLU:x-=XSPEED;y-=YSPEED;break;caseU:y-=YSPEED;break;caseRU:x+=XSPEED;y-=YSPEED;break;caseR:x+=XSPEED;break;caseRD:x+=XSPEED;y+=YSPEED;break;caseD:y+=YSPEED;break;caseLD:x-=XSPEED;y+=YSPEED;break;}if(x<0||y<0||x>TankClient.GAME_WIDTH||y>TankClient.GAME_HEIGHT){live=false;}}publicRectanglegetRect(){;returnnewRectangle(x,y,WIDTH,HEIGHT);}publicbooleanhitTank(Tankt){if(this.live&&t.isLive()&&this.good!=t.good&&this.getRect().intersects(t.getRect())){this.live=false;t.setLive(false);tc.explodes.add(newExplode(x,y,tc));returntrue;}returnfalse;}publicbooleanhitTanks(List<Tank>tanks){for(inti=0;i<tanks.size();i++){if(this.hitTank(tanks.get(i))){returntrue;}}returnfalse;}publicbooleanisLive(){returnlive;}}//class?Explodeimportjava.awt.Color;importjava.awt.Graphics;publicclassExplode{intx,y;privatebooleanlive=true;privateTankClienttc;privateint[]diameter={4,7,12,18,26,32,49,30,14,6};intstep=0;publicExplode(intx,inty,TankClienttc){this.x=x;this.y=y;this.tc=tc;}publicvoiddraw(Graphicsg){if(!live){tc.explodes.remove(this);return;}Colorc=g.getColor();g.setColor(Color.black);g.fillOval(x,y,diameter[step],diameter[step]);g.setColor(c);step++;if(step==diameter.length){live=false;step=0;return;}}}//classenumpublicenumDirection{U,RU,R,RD,D,LD,L,LU,STOP}//class?Msgimportjava.io.DataInputStream;importjava.net.DatagramSocket;publicinterfaceMsg{publicstaticfinalintTANK_NEW_MSG=1;publicstaticfinalintTANK__MOVE_MSG=2;publicstaticfinalintMISSILE_NEW_MSG=3;publicstaticfinalintTANK_DEAD_MSG=4;publicstaticfinalintMISSILE_DEAD_MSG=5;publicvoidsend(DatagramSocketds,StringIP,intudpPort);publicvoidparse(DataInputStreamdis);}
解决方案三:
//classNetClientimportjava.io.ByteArrayInputStream;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.IOException;importjava.net.DatagramPacket;importjava.net.DatagramSocket;importjava.net.Socket;importjava.net.SocketException;importjava.net.UnknownHostException;publicclassNetClient{TankClienttc;privateintudpPort;StringIP;DatagramSocketds=null;publicNetClient(TankClienttc){this.tc=tc;}publicvoidconnect(StringIP,intport){this.IP=IP;try{ds=newDatagramSocket(udpPort);}catch(SocketExceptione){e.printStackTrace();}Sockets=null;try{s=newSocket(IP,port);DataOutputStreamdos=newDataOutputStream(s.getOutputStream());dos.writeInt(udpPort);DataInputStreamdis=newDataInputStream(s.getInputStream());intid=dis.readInt();tc.myTank.id=id;if(id%2==0){tc.myTank.good=false;}else{tc.myTank.good=true;}System.out.println("ConnectedtoServer");}catch(UnknownHostExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}finally{if(s!=null){try{s.close();s=null;}catch(IOExceptione){e.printStackTrace();}}}TankNewMsgmsg=newTankNewMsg(tc.myTank);send(msg);newThread(newUDPRecvThread()).start();}publicvoidsend(Msgmsg){msg.send(ds,IP,TankServer.UDP_PORT);}privateclassUDPRecvThreadimplementsRunnable{byte[]buf=newbyte[1024];publicvoidrun(){while(ds!=null){DatagramPacketdp=newDatagramPacket(buf,buf.length);try{ds.receive(dp);parse(dp);System.out.println("apackagereceivefromserver");}catch(IOExceptione){e.printStackTrace();}}}publicvoidparse(DatagramPacketdp){ByteArrayInputStreambais=newByteArrayInputStream(buf,0,dp.getLength());DataInputStreamdis=newDataInputStream(bais);intmsgType=0;try{msgType=dis.readInt();}catch(IOExceptione){e.printStackTrace();}Msgmsg=null;switch(msgType){caseMsg.TANK_NEW_MSG:msg=newTankNewMsg(NetClient.this.tc);msg.parse(dis);break;caseMsg.TANK__MOVE_MSG:msg=newTankMoveMsg(NetClient.this.tc);msg.parse(dis);break;caseMsg.MISSILE_NEW_MSG:msg=newMissileNewMsg(NetClient.this.tc);msg.parse(dis);break;caseMsg.TANK_DEAD_MSG:msg=newTankDeadMsg(NetClient.this.tc);msg.parse(dis);break;caseMsg.MISSILE_DEAD_MSG:msg=newMissileDeadMsg(NetClient.this.tc);msg.parse(dis);break;}}}publicintgetUdpPort(){returnudpPort;}publicvoidsetUdpPort(intudpPort){this.udpPort=udpPort;}}//class?MissileNewMsgimportjava.io.ByteArrayOutputStream;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.IOException;importjava.net.DatagramPacket;importjava.net.DatagramSocket;importjava.net.InetSocketAddress;importjava.net.SocketException;publicclassMissileNewMsgimplementsMsg{intmsgType=Msg.MISSILE_NEW_MSG;TankClienttc;Missilem;publicMissileNewMsg(Missilem){this.m=m;}publicMissileNewMsg(TankClienttc){this.tc=tc;}publicvoidsend(DatagramSocketds,StringIP,intudpPort){ByteArrayOutputStreambaos=newByteArrayOutputStream();DataOutputStreamdos=newDataOutputStream(baos);try{dos.writeInt(msgType);dos.writeInt(m.tankId);dos.writeInt(m.id);dos.writeInt(m.x);dos.writeInt(m.y);dos.writeInt(m.dir.ordinal());dos.writeBoolean(m.good);}catch(IOExceptione){e.printStackTrace();}byte[]buf=baos.toByteArray();try{DatagramPacketdp=newDatagramPacket(buf,buf.length,newInetSocketAddress(IP,udpPort));ds.send(dp);}catch(SocketExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}publicvoidparse(DataInputStreamdis){try{inttankId=dis.readInt();if(tankId==tc.myTank.id){return;}intid=dis.readInt();intx=dis.readInt();inty=dis.readInt();Directiondir=Direction.values()[dis.readInt()];booleangood=dis.readBoolean();Missilem=newMissile(tankId,x,y,good,dir,tc);m.id=id;tc.missile.add(m);}catch(IOExceptione){e.printStackTrace();}}}//class?MissileDeadMsgimportjava.io.ByteArrayOutputStream;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.IOException;importjava.net.DatagramPacket;importjava.net.DatagramSocket;importjava.net.InetSocketAddress;importjava.net.SocketException;publicclassMissileDeadMsgimplementsMsg{intmsgType=Msg.TANK_DEAD_MSG;TankClienttc;inttankId;intid;publicMissileDeadMsg(inttankId,intid){this.tankId=tankId;this.id=id;}publicMissileDeadMsg(TankClienttc){this.tc=tc;}publicvoidparse(DataInputStreamdis){try{inttankId=dis.readInt();intid=dis.readInt();if(tc.myTank.id==tankId){return;}for(inti=0;i<tc.missile.size();i++){Missilem=tc.missile.get(i);if(m.tankId==tankId&&m.id==id){m.live=false;tc.explodes.add(newExplode(m.x,m.y,tc));break;}}}catch(IOExceptione){e.printStackTrace();}}publicvoidsend(DatagramSocketds,StringIP,intudpPort){ByteArrayOutputStreambaos=newByteArrayOutputStream();DataOutputStreamdos=newDataOutputStream(baos);try{dos.writeInt(msgType);dos.writeInt(tankId);dos.writeInt(id);}catch(IOExceptione){e.printStackTrace();}byte[]buf=baos.toByteArray();try{DatagramPacketdp=newDatagramPacket(buf,buf.length,newInetSocketAddress(IP,udpPort));ds.send(dp);}catch(SocketExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}}//classTankNewMsgimportjava.io.ByteArrayOutputStream;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.IOException;importjava.net.DatagramPacket;importjava.net.DatagramSocket;importjava.net.InetSocketAddress;importjava.net.SocketException;publicclassTankNewMsgimplementsMsg{intmsgType=Msg.TANK_NEW_MSG;Tanktank;TankClienttc;publicTankNewMsg(Tanktank){this.tank=tank;}publicTankNewMsg(TankClienttc){this.tc=tc;}publicvoidsend(DatagramSocketds,StringIP,intudpPort){ByteArrayOutputStreambaos=newByteArrayOutputStream();DataOutputStreamdos=newDataOutputStream(baos);try{dos.writeInt(msgType);dos.writeInt(tank.id);dos.writeInt(tank.x);dos.writeInt(tank.y);dos.writeInt(tank.dir.ordinal());dos.writeBoolean(tank.good);}catch(IOExceptione){e.printStackTrace();}byte[]buf=baos.toByteArray();try{DatagramPacketdp=newDatagramPacket(buf,buf.length,newInetSocketAddress(IP,udpPort));ds.send(dp);}catch(SocketExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}publicvoidparse(DataInputStreamdis){try{intid=dis.readInt();if(tc.myTank.id==id){return;}intx=dis.readInt();inty=dis.readInt();Directiondir=Direction.values()[dis.readInt()];booleangood=dis.readBoolean();//System.out.println("id:"+id+"-x:"+x+"-y:"+y+"-dir:"+dir+"-good"+good);booleanexist=false;for(inti=0;i<tc.tanks.size();i++){Tankt=tc.tanks.get(i);if(t.id==id){exist=true;break;}}if(!exist){TankNewMsgtnMsg=newTankNewMsg(tc.myTank);tc.nc.send(tnMsg);Tankt=newTank(x,y,good,dir,tc);t.id=id;tc.tanks.add(t);}}catch(IOExceptione){e.printStackTrace();}}}//class?TankDeadMsgimportjava.io.ByteArrayOutputStream;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.IOException;importjava.net.DatagramPacket;importjava.net.DatagramSocket;importjava.net.InetSocketAddress;importjava.net.SocketException;publicclassTankDeadMsgimplementsMsg{intmsgType=Msg.TANK_DEAD_MSG;TankClienttc;intid;publicTankDeadMsg(intid){this.id=id;}publicTankDeadMsg(TankClienttc){this.tc=tc;}publicvoidparse(DataInputStreamdis){try{intid=dis.readInt();if(tc.myTank.id==id){return;}for(inti=0;i<tc.tanks.size();i++){Tankt=tc.tanks.get(i);if(t.id==id){t.setLive(false);;break;}}}catch(IOExceptione){e.printStackTrace();}}publicvoidsend(DatagramSocketds,StringIP,intudpPort){ByteArrayOutputStreambaos=newByteArrayOutputStream();DataOutputStreamdos=newDataOutputStream(baos);try{dos.writeInt(msgType);dos.writeInt(id);}catch(IOExceptione){e.printStackTrace();}byte[]buf=baos.toByteArray();try{DatagramPacketdp=newDatagramPacket(buf,buf.length,newInetSocketAddress(IP,udpPort));ds.send(dp);}catch(SocketExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}}
解决方案四:
//classTankMoveMsgimportjava.io.ByteArrayOutputStream;importjava.io.DataInputStream;importjava.io.DataOutputStream;importjava.io.IOException;importjava.net.DatagramPacket;importjava.net.DatagramSocket;importjava.net.InetSocketAddress;importjava.net.SocketException;publicclassTankMoveMsgimplementsMsg{intmsgType=Msg.TANK__MOVE_MSG;intx,y;intid;DirectionptDir;Directiondir;TankClienttc;publicTankMoveMsg(intid,intx,inty,Directiondir,DirectionptDir){this.id=id;this.x=x;this.y=y;this.dir=dir;this.ptDir=ptDir;}publicTankMoveMsg(TankClienttc){this.tc=tc;}publicvoidparse(DataInputStreamdis){try{intid=dis.readInt();if(tc.myTank.id==id){return;}intx=dis.readInt();inty=dis.readInt();Directiondir=Direction.values()[dis.readInt()];DirectionptDir=Direction.values()[dis.readInt()];//System.out.println("id:"+id+"-x:"+x+"-y:"+y+"-dir:"+dir+"-good"+good);booleanexist=false;for(inti=0;i<tc.tanks.size();i++){Tankt=tc.tanks.get(i);if(t.id==id){t.x=x;t.y=y;t.dir=dir;t.ptDir=ptDir;exist=true;break;}}}catch(IOExceptione){e.printStackTrace();}}publicvoidsend(DatagramSocketds,StringIP,intudpPort){ByteArrayOutputStreambaos=newByteArrayOutputStream();DataOutputStreamdos=newDataOutputStream(baos);try{dos.writeInt(msgType);dos.writeInt(id);dos.writeInt(x);dos.writeInt(y);dos.writeInt(dir.ordinal());dos.writeInt(ptDir.ordinal());}catch(IOExceptione){e.printStackTrace();}byte[]buf=baos.toByteArray();try{DatagramPacketdp=newDatagramPacket(buf,buf.length,newInetSocketAddress(IP,udpPort));ds.send(dp);}catch(SocketExceptione){e.printStackTrace();}catch(IOExceptione){e.printStackTrace();}}}
解决方案五:
根本看不完
解决方案六:
自己尝试调试解决吧
解决方案七:
代码太长。楼主先学会怎么去问问题
解决方案八:
如果是根据视频学的话,那你写的代码你肯定知道是哪部分的,你说另外一端的client不消失,那你把client消失的代码拷出来就行了,把全部代码拉下来没人看的。
解决方案九:
我只能说,兄弟,主意形象,要考虑到看帖人的感受
解决方案十:
我擦从来没见过这么牛逼的帖子
解决方案十一:
AWT/Swing是很老的javaGUI编程接口,建议楼主不要再在这上面花精力了,现在主流都是J2EE,即使是桌面应用也是SWT/Jface的天下
解决方案十二:
如果这些代码是楼主又敲的,而不是复制的话,我猜楼主应该已经自己找到问题了吧
解决方案十三:
楼主,膜拜你。
解决方案十四:
直接吓尿了
解决方案十五:
不考虑别人的感受,连代码引用都没加。
解决方案:
不写点注释?