import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; import java.net.*; import java.applet.*; import javax.swing.*; class uus_klient implements ActionListener, KeyListener{ //Globaalsed muutujad Socket sc; String S_yks=""; String S_kaks=""; int x1=150; int y1=150; int x2=120; int y2=120; Ala ala=new Ala(); PrintWriter väljund; BufferedReader sisend; boolean bool=false; JTextField TF_nimi=new JTextField(10); class Ala extends Applet{ public void paint(Graphics g){ g.drawString(S_yks,x1,y1); g.drawString(S_kaks,x2,y2); System.out.println("paint"); } } class Lõim extends Thread{ public Lõim(){ start(); } public void run(){ System.out.println("Lõim töötab."); try{ sisend=new BufferedReader(new InputStreamReader(sc.getInputStream())); while(true){ Vector temp=new Vector(); String rida=sisend.readLine(); StringTokenizer st=new StringTokenizer(rida,";"); while(st.hasMoreTokens()){ temp.add(st.nextToken()); } String temp0=(String)temp.elementAt(0); System.out.println("temp0 on "+temp0); String S_tempx=(String)temp.elementAt(1); String S_tempy=(String)temp.elementAt(2); String tempnimi=(String)temp.elementAt(3); int tempx=Integer.parseInt(S_tempx); int tempy=Integer.parseInt(S_tempy); if(temp0.equals("yks")){ x1=tempx; y1=tempy; S_yks=tempnimi; }else{ x2=tempx; y2=tempy; S_kaks=tempnimi; } System.out.println("x1="+x1+" y1="+y1+" x2="+x2+" y2="+y2+" nimi="+tempnimi); ala.repaint(); } }catch(Exception e){System.out.println("Viga: "+e);} } } public void keyTyped(KeyEvent ke){} public void keyPressed(KeyEvent ke){ System.out.println("Saabus KeyEvent"); try{ väljund=new PrintWriter(sc.getOutputStream(),true); }catch(Exception e){System.out.println("Viga: "+e);} if(bool){ if(ke.getKeyCode()==38){ y2--; System.out.println("ülesse"); ala.repaint(); } if(ke.getKeyCode()==40){ y2++; System.out.println("alla"); ala.repaint(); } if(ke.getKeyCode()==37){ x2--; System.out.println("vasakule"); ala.repaint(); } if(ke.getKeyCode()==39){ x2++; System.out.println("paremale"); ala.repaint(); } väljund.println("kaks;"+x2+";"+y2+";"+S_kaks+";"); }else{ if(ke.getKeyCode()==38){ y1--; System.out.println("ülesse"); ala.repaint(); } if(ke.getKeyCode()==40){ y1++; System.out.println("alla"); ala.repaint(); } if(ke.getKeyCode()==37){ x1--; System.out.println("vasakule"); ala.repaint(); } if(ke.getKeyCode()==39){ x1++; System.out.println("paremale"); ala.repaint(); } väljund.println("yks;"+x1+";"+y1+";"+S_yks+";"); } } public void keyReleased(KeyEvent ke){} public void actionPerformed(ActionEvent e){ if(e.getActionCommand()=="ühenda"){ try{ sc=new Socket("localhost",22222); sisend=new BufferedReader(new InputStreamReader(sc.getInputStream())); String temp=sisend.readLine(); if(temp.equals("Esimene")){ bool=true; System.out.println("Klient: Üks peaks juba olemas olema"); }else{ System.out.println("Klient: See on esimene"); } väljund=new PrintWriter(sc.getOutputStream(),true); if(bool){ S_kaks=TF_nimi.getText(); väljund.println(S_kaks); System.out.println("Nimi saadeti ära."); }else{ S_yks=TF_nimi.getText(); väljund.println(S_yks); System.out.println("Nimi saadeti ära."); } new Lõim(); }catch(Exception ex){System.out.println("Ühendamisega on jama: "+ex);} } } public Container createContentPane(){ JPanel content=new JPanel(new BorderLayout()); JPanel alumine=new JPanel(); JLabel L_nimi=new JLabel("Nimi: "); JButton B_yhenda=new JButton("Ühenda"); B_yhenda.setActionCommand("ühenda"); B_yhenda.addActionListener(this); ala.addKeyListener(this); alumine.add(L_nimi); alumine.add(TF_nimi); alumine.add(B_yhenda); content.add(ala); content.add(alumine, BorderLayout.SOUTH); return content; } public static void main(String args[]){ JFrame.setDefaultLookAndFeelDecorated(true); JFrame aken=new JFrame("Klient"); aken.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); uus_klient k=new uus_klient(); aken.setContentPane(k.createContentPane()); aken.setSize(480,320); aken.setLocation(100,100); aken.show(); } }