import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TenniseKlient extends JPanel implements ActionListener, Runnable, KeyListener{ JTextField masin=new JTextField("localhost"); JTextField varat=new JTextField("3001"); JButton sisenemisnupp=new JButton("Sisene"); JPanel ylapaneel=new JPanel(new GridLayout(1, 2)); TenniseManguPlats plats=new TenniseManguPlats(); Socket sc; PrintWriter valja; BufferedReader sisse; public TenniseKlient(){ ylapaneel.add(masin); JPanel abipaneel=new JPanel(new GridLayout(1, 2)); abipaneel.add(varat); abipaneel.add(sisenemisnupp); ylapaneel.add(abipaneel); setLayout(new BorderLayout()); add(ylapaneel, BorderLayout.NORTH); add(plats, BorderLayout.CENTER); sisenemisnupp.addActionListener(this); } public void run(){ try{ while(true){ String rida=sisse.readLine(); if(rida.startsWith(".asukoht ")){ String m[]=rida.split(" "); plats.asukohad(Integer.parseInt(m[1]), Integer.parseInt(m[2]), Integer.parseInt(m[3]), Integer.parseInt(m[4])); } } }catch(Exception ex){ex.printStackTrace();} } public void keyPressed(KeyEvent e){ int kood=e.getKeyCode(); if(kood==KeyEvent.VK_LEFT){ valja.println(".vasakule"); } if(kood==KeyEvent.VK_RIGHT){ valja.println(".paremale"); } } public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} public void actionPerformed(ActionEvent e){ if(e.getSource()==sisenemisnupp){ try{ sc=new Socket(masin.getText(), Integer.parseInt(varat.getText())); valja=new PrintWriter(sc.getOutputStream(), true); sisse=new BufferedReader(new InputStreamReader(sc.getInputStream())); remove(ylapaneel); validate(); plats.addKeyListener(this); new Thread(this).start(); } catch(Exception ex){ex.printStackTrace();} } } public static void main(String[] arg){ JFrame f=new JFrame("Tennis"); f.add(new TenniseKlient()); f.setSize(400, 400); f.setVisible(true); } }