import java.applet.Applet; import java.awt.event.*; import java.awt.*; import java.net.*; import java.io.*; import java.util.*; public class Vorguliiklus2 extends Applet implements KeyListener, Runnable{ BufferedReader sisse; PrintWriter valja; String masin="localhost"; int port=3001; String nimi="Jaagup"; public Vorguliiklus2(){ try{ addKeyListener(this); Socket sc=new Socket(masin, port); valja=new PrintWriter(sc.getOutputStream(), true); sisse=new BufferedReader( new InputStreamReader(sc.getInputStream()) ); System.out.println(sisse.readLine()); valja.println(nimi); new Thread(this).start(); }catch(Exception viga){ viga.printStackTrace(); } } public void paint(Graphics g){ } public void run(){ while(true){ try{ String rida=sisse.readLine(); StringTokenizer stk=new StringTokenizer(rida); int x=Integer.parseInt(stk.nextToken()); int y=Integer.parseInt(stk.nextToken()); String nimi=stk.nextToken(); getGraphics().drawString(nimi, x, y); }catch(Exception viga){ System.out.println(viga); } } } 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"); } if(kood==KeyEvent.VK_UP){ valja.println(".yles"); } if(kood==KeyEvent.VK_DOWN){ valja.println(".alla"); } } public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} public static void main(String argumendid[]){ Frame f=new Frame("Klahvikuular"); f.add(new Vorguliiklus2()); f.setSize(300, 300); f.setVisible(true); } }