新人求高手帮助,搞了两天没搞懂

问题描述

跟着马士兵视频学得坦克大战网络版,找不到问题在哪求高手帮助两个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的天下
解决方案十二:
如果这些代码是楼主又敲的,而不是复制的话,我猜楼主应该已经自己找到问题了吧
解决方案十三:
楼主,膜拜你。
解决方案十四:
直接吓尿了
解决方案十五:
不考虑别人的感受,连代码引用都没加。
解决方案:
不写点注释?

时间: 2024-10-03 09:03:55

新人求高手帮助,搞了两天没搞懂的相关文章

c++ poj-求大家帮我看看poj的1723,提交后告诉我答案错误,为什么?我一直没搞懂poj的编译器的规则

问题描述 求大家帮我看看poj的1723,提交后告诉我答案错误,为什么?我一直没搞懂poj的编译器的规则 #include #include using namespace std; int round(double sum int num) { if(sum>0) return (int)(sum/num + 0.5); else if(sum == 0) return 0; else return (int)(sum/num - 0.5);} bool isexit(int* x int y

跪求高手赐教,两个数据库两个表怎样象SQL中做Left join关联然后在datagrid中输出!!!!

问题描述 目前有两个数据库,一个ORACLE一个sqlserver,想比较两个数据库中两张表的差异,并把结果输出到datagrid,能不能实现象在SQL中LEFTJOIN那样的效果进行关联?(DTS做起来效率太低)DATASETDATATABLE可以实现这样的效果吗?查阅了很多资料和MSDN,都没找到答案,求高手赐教!!!最好是有思路和代码,谢谢!!! 解决方案 解决方案二:关注-解决方案三:明确的告诉你不行!解决方案四:呜呜....连变通的方法也没有吗?解决方案五:比较两个数据库中两张表的差异

如何在两小时内搞懂PHOTOSHOP基础工具?

  初学者面对Photoshop这个庞然大物,第一直观感受是不知道从何下手.那么多工具,学完这个忘那个,有木有什么教程可以一次性学完呢?今天这篇好文,只要童鞋们打起精神来,两个小时就可以搞懂基础的工具使用!绝对是新手速成Photoshop的一个好机会!赶紧来学习! 教程很有意思,从A--Z 依次讲解每个工具的使用,精辟彻底,不拖泥带水呦. 3D面板:作为Adobe Photoshop Extended的一部分,你可以在Adobe Photoshop CC中找到它.3D面板中展示了所有3D文件中允

.net序列化问题,实在没搞懂

问题描述 本来工程师好好的,但是今天打开的时候突然报这个错误,怎么回事呢?--------------------------------------------------------------------------------Type'System.Data.Common.DbParameter'inAssembly'System.Data,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089'isnotmarke

新人求高手指点学习ECLISPE开发jsp方法

问题描述 小的没接触过此类编程语言,所以想从一个已完成的项目入手琢磨,现从网上找到一个源代码,但是琢磨了半天也不能把这个项目配置到正确运行,现特意上网求助,这个是一个英语在线学习系统,内置的帮助文件如下------------------------------------------------名称:复读365英语在线学习平台1.2版开发工具:eclipse采用技术:j2ee,struts,hibernate,spring,jsp,jquery,ajax运行环境:jdk6.0,tomcat6,

发布tomcat项目,搞了一个晚上没搞定!!!,急急急

问题描述 想通过:http://211.149.78.63ip访问外网的tomcat下的项目.我设置如下:1.将myerp复制到服务器的tomcat/webapps/目录下2.在服务器tomcat/conf/server.xml设置如下端口已改为80<Connectorport="80"protocol="HTTP/1.1"...............关键是设置下面(按照网上的参考)<Hostname="211.149.78.63"

程序性能分析问题,求高手帮助。

问题描述 向各位大侠求助..现在正在维护一个系统,是为其他系统提供单点登录(SSO)的一个系统,没有业务,就是一个简单的画面.系统在2015年前,当时用的server操作系统是hpunix,web服务器是websphere,java的servlet开发,系统架构上稍微复杂一些,是有四台机器提供认证服务,其中没台机器都会定时向其他server提供情报(session)共有客户端对server的一次认证,当服务器忙或者无响应的时候,会得到返回时,由工作于客户端module(也是我们开发)自动转给第二

源代码-寻求帮助!C++程序,无法通过编译,我没检查出错误,求高手相助!

问题描述 寻求帮助!C++程序,无法通过编译,我没检查出错误,求高手相助! C++ primer plus 6th edition 课后编程联系,p163页,第一题. 题目要求输入两个整数,并输出这两个整数之间所有整数值的和. #include int main() { using namespace std; cout << "PLEASE ENTER 2 NUMBERS: "; //声明两个整数变量,用来保存输入的值 int number1, number2; //wh

html5-【菜鸟问题】两组轮播JS代码有冲突,问题很多!求高手帮忙解决

问题描述 [菜鸟问题]两组轮播JS代码有冲突,问题很多!求高手帮忙解决 上图JS代码如下: [code=javascript] //首页顶部轮播 var i = 0; jummper(); function jummper() { $(".car ul li").eq(i).find("img").css("left", "-100%"); //滑动图在左边-100%准备 $(".car ul li").e