import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.net.*; import java.io.*; import java.util.*; public class Klient2 extends Applet implements ActionListener, Runnable, MouseListener{ Button nupp=new Button("Ühenda"); TextField tf=new TextField(); TextArea ta=new TextArea(); Panel paigutusAla=new JoonistusPaneel(); Panel keskOsa=new Panel(new GridLayout(1, 2)); Hashtable asukohad=new Hashtable(); PrintWriter valja; BufferedReader sisse; boolean sees=false; public Klient2(){ setLayout(new BorderLayout()); add(nupp, BorderLayout.NORTH); keskOsa.add(ta); keskOsa.add(paigutusAla); add(keskOsa, BorderLayout.CENTER); add(tf, BorderLayout.SOUTH); nupp.addActionListener(this); tf.addActionListener(this); paigutusAla.addMouseListener(this); tf.setEnabled(false); } public void mousePressed(MouseEvent e){ valja.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 void actionPerformed(ActionEvent e){ try{ if(e.getSource()==nupp){ if(sees){ valja.println(".ots"); } else { Socket sc=new Socket("ws0.cs1.tpu.ee", 3001); valja=new PrintWriter(sc.getOutputStream(), true); sisse=new BufferedReader(new InputStreamReader( sc.getInputStream())); tf.setEnabled(true); sees=true; nupp.setLabel("Lahku"); new Thread(this).start(); } } if(e.getSource()==tf){ valja.println(tf.getText()); tf.setText(""); } } catch(Exception ex){ex.printStackTrace();} } public void run(){ try{ for(String rida=sisse.readLine(); rida!=null; rida=sisse.readLine()){ try{ StringTokenizer stk=new StringTokenizer(rida); String kasutajanimi=stk.nextToken(); Punkt p=new Punkt(); p.x=Integer.parseInt(stk.nextToken()); p.y=Integer.parseInt(stk.nextToken()); asukohad.put(kasutajanimi, p); paigutusAla.repaint(); }catch(Exception ex){} ta.append(rida+"\n"); } }catch(Exception e){e.printStackTrace();} sees=false; nupp.setLabel("Ühenda"); } public static void main(String[] argumendid){ Frame f=new Frame("Jututoa graafiline klient"); f.add(new Klient2()); f.setSize(300, 300); f.setVisible(true); } class Punkt{ int x; int y; } class JoonistusPaneel extends Panel{ public void paint(Graphics g){ Enumeration kasutajanimed=asukohad.keys(); while(kasutajanimed.hasMoreElements()){ String kasutajanimi=(String)kasutajanimed.nextElement(); Punkt asukoht=(Punkt)asukohad.get(kasutajanimi); g.drawString(kasutajanimi, asukoht.x, asukoht.y); } } } }