import javax.swing.*; import java.awt.event.*; import java.net.*; import java.io.*; import java.util.*; public class Client extends JPanel implements Runnable, KeyListener{ Socket sc; PrintWriter pw; BufferedReader br; boolean working=true; Map locations=new HashMap<>(); String message=""; public Client(){ addKeyListener(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(); setFocusable(true); requestFocus(); } catch(Exception ex){ex.printStackTrace();} } public void keyPressed(KeyEvent e){ if(e.getKeyCode()==KeyEvent.VK_UP){pw.println("f");} if(e.getKeyCode()==KeyEvent.VK_RIGHT){pw.println("r");} } public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} public void run(){ try{ while(working){ String line=br.readLine(); try{ String[] m1=line.split(","); if(m1.length==4){ locations.put(m1[0], new int[]{ Integer.parseInt(m1[1]), Integer.parseInt(m1[2]), Integer.parseInt(m1[3]) }); repaint(); } } catch(Exception ex){ex.printStackTrace();} } } catch(Exception ex){ex.printStackTrace(); message="Server ended"; repaint(); } } public void paintComponent(java.awt.Graphics g){ super.paintComponent(g); g.drawString("Locations:", 100, 10); for(String username: locations.keySet()){ int[] location=locations.get(username); g.drawString(username, location[0], location[1]); int step=25; int linex=(int)(location[0]+step*Math.cos(Math.toRadians(location[2]))); int liney=(int)(location[1]+step*Math.sin(Math.toRadians(location[2]))); g.drawLine(location[0], location[1], linex, liney); } g.drawString(message, 100, 100); } public static void main(String[] args){ JFrame f=new JFrame("Client"); f.getContentPane().add(new Client()); f.setSize(400, 300); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }