java - Drawing function messes up, when creating connection between server and client -


i'm working a tcpclient class, , tcpserver class. i've created drawing program in tcpclient, , sending point(drawing) information server, , vice-versa, make clients running on server work on "same" canvas.

tcpserver:

public class tcpserver {      private static final int serverport = 9000;      public static void main(string argv[]) throws exception {         serversocket welcomesocket = new serversocket(serverport);          arraylist<point> completedrawing = new arraylist<>();         arraylist<point> receivedlist = new arraylist<>();          while (true) {              socket connectionsocket = welcomesocket.accept();             objectinputstream infromclient = new objectinputstream(connectionsocket.getinputstream());               objectoutputstream outtoclient = new objectoutputstream(connectionsocket.getoutputstream());              receivedlist.addall((arraylist<point>) infromclient.readobject());             completedrawing.addall(receivedlist);             receivedlist.clear();              outtoclient.writeobject(completedrawing);              connectionsocket.close();         }      } } 

tcpclient:

public class tcpclient extends jpanel {      public static arraylist<point> location = new arraylist<>();      private jtextarea consoleoutput = new jtextarea(1,20);      public void addcomponenttopane(container pane) {         consoleoutput.seteditable(false);     }      public tcpclient() {         addmouselistener(new mouseadapter() {             @override             public void mousepressed(mouseevent e) {                 synchronized (location) {                 location.add(e.getpoint());                 location.notify();                 }             }         });          addmousemotionlistener(new mousemotionadapter() {             @override             public void mousedragged(mouseevent e) {                 synchronized (location) {                 location.add(e.getpoint());                 location.notify();                 repaint();                 }             }         });          setpreferredsize(new dimension(800, 500));         setbackground(color.white);     }      @override     protected void paintcomponent(graphics g) {         super.paintcomponent(g);          synchronized (location) {             if(location.isempty()){                 return;             }              point p = location.get(0);             (int = 1; < location.size(); i++) {                 point q = location.get(i);                 g.drawline(p.x, p.y, q.x, q.y);                 p = q;             }         }     }      public static void main(string argv[])  throws exception {          inetaddress serverip = inetaddress.getlocalhost();          jframe frame = new jframe("drawing friends");         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.add(new tcpclient(), borderlayout.center);          jtextarea ipadress = new jtextarea(1,20);         ipadress.seteditable(false);         ipadress.append("device ip: " + serverip.gethostaddress());         frame.add(ipadress, borderlayout.south);          frame.setsize(new dimension(800,600));         frame.setlocationrelativeto(null);         frame.setresizable(false);         frame.setvisible(true);          while(true) {             synchronized (location) {                 socket clientsocket = new socket("localhost", 9000);                  objectoutputstream outtoserver = new objectoutputstream(clientsocket.getoutputstream());                 objectinputstream infromserver = new objectinputstream(clientsocket.getinputstream());                  outtoserver.writeobject(location);                  outtoserver.flush();                 location.wait();                  location.addall((arraylist<point>) infromserver.readobject());             }         }     } } 

with these classes, drawing function messes up. start lagging , stuttering hard, while drawing works 1 point: enter image description here

however, if comment out server client stream, works fine (only in 1 client window of course): enter image description here

i have suspesion issue related client being overflowed data, besides not know how work around issue.

there many data on client, because data in 'drawing' arraylists (on both sides) have lot duplications. rewrite code add new points 'location' , 'completedrawing' lists like:

arraylist<point> serverpoints = (arraylist<point>)infromserver.readobject(); for(point p : serverpoints){   if(!location.contains(p)) location.add(p); } 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -