import javax.swing.*; import java.awt.event.*; import java.net.*; import java.io.*; import java.util.*; public class Client3 extends JPanel implements MouseListener, Runnable{ Socket sc; PrintWriter pw; BufferedReader br; boolean working=true; Map locations=new HashMap<>(); String message=""; public Client3(){ addMouseListener(this); try{ sc=new Socket("192.168.2.231", 3001); pw=new PrintWriter(sc.getOutputStream(), true); pw.println("Jaagup"); br=new BufferedReader(new InputStreamReader(sc.getInputStream())); new Thread(this).start(); } catch(Exception ex){ex.printStackTrace();} } public void run(){ try{ while(working){ String line=br.readLine(); try{ String[] m1=line.split(":"); if(m1.length==2){ String[] m2=m1[1].trim().split(" "); locations.put(m1[0], new java.awt.Point( Integer.parseInt(m2[0]), Integer.parseInt(m2[1]) )); System.out.println(locations); repaint(); } } catch(Exception ex){ex.printStackTrace();} } } catch(Exception ex){ex.printStackTrace(); // this.getGraphics().drawString("Server ended", 100, 100); message="Server ended"; repaint(); } } //draw locations to screen public void paintComponent(java.awt.Graphics g){ super.paintComponent(g); g.drawString("Locations:", 100, 10); for(String username: locations.keySet()){ java.awt.Point location=locations.get(username); g.drawString(username, (int)location.getX(), (int)location.getY()); } g.drawString(message, 100, 100); } public void mousePressed(MouseEvent e){ System.out.println(e.getX()+" "+e.getY()); pw.println(e.getX()+" "+e.getY()); } public void mouseReleased(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public static void main(String[] args){ JFrame f=new JFrame("Client"); f.getContentPane().add(new Client3()); f.setSize(400, 300); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }