import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.util.*; import com.sun.j3d.utils.universe.SimpleUniverse; import com.sun.j3d.utils.geometry.*; import javax.media.j3d.*; import javax.vecmath.*; public class JalutustoaKlient extends Applet implements Runnable, ActionListener, KeyListener{ Hashtable kasutajad=new Hashtable(); Joonistusala j=new Joonistusala(this); Canvas3D c3d=new Canvas3D(SimpleUniverse.getPreferredConfiguration()); BranchGroup juur=new BranchGroup(); TextField tf=new TextField(); PrintWriter vorku; BufferedReader vorgust; public JalutustoaKlient(){ setLayout(new BorderLayout()); Panel sisepaneel=new Panel(new GridLayout(2, 1)); sisepaneel.add(j); sisepaneel.add(c3d); j.addKeyListener(this); c3d.addKeyListener(this); add(sisepaneel, BorderLayout.CENTER); juur.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE); // juur.setCapability(TransformGroup.ALLOW_CHILDREN_REMOVE); // juur.addChild(new ColorCube(0.4)); juur.compile(); SimpleUniverse u = new SimpleUniverse(c3d); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(juur); add(tf, BorderLayout.NORTH); tf.addActionListener(this); try{ Socket sc=new Socket("ws0.cs1.tlu.ee", 3001); vorku=new PrintWriter(sc.getOutputStream(), true); vorgust=new BufferedReader(new InputStreamReader(sc.getInputStream())); new Thread(this).start(); }catch(Exception ex){ ex.printStackTrace(); } } public void run(){ try{ String rida=vorgust.readLine(); while(rida!=null){ System.out.println(rida); if(rida.startsWith(".asukoht")){ String[] m=rida.split(" "); KlientKasutaja k=kasutajad.get(m[1]); if(k==null){ k=new KlientKasutaja(Integer.parseInt(m[2]), Integer.parseInt(m[3]), Integer.parseInt(m[4]), Double.parseDouble(m[5])); juur.insertChild(k.kysiGrupp(), 0); kasutajad.put(m[1], k); } else { k.muudaAsukoht(Integer.parseInt(m[2]), Integer.parseInt(m[3]), Integer.parseInt(m[4]), Double.parseDouble(m[5])); } } if(rida.startsWith(".lahkus ")){ String[] m=rida.split(" "); KlientKasutaja k=kasutajad.get(m[1]); //juur.removeChild(k.kysiGrupp()); kasutajad.remove(m[1]); } j.repaint(); rida=vorgust.readLine(); } }catch(Exception ex){ ex.printStackTrace(); } } public void actionPerformed(ActionEvent e){ vorku.println(tf.getText()); } public void keyPressed(KeyEvent e){ if(e.getKeyCode()==KeyEvent.VK_LEFT){vorku.println(".vasakule");} if(e.getKeyCode()==KeyEvent.VK_RIGHT){vorku.println(".paremale");} if(e.getKeyCode()==KeyEvent.VK_UP){vorku.println(".yles");} if(e.getKeyCode()==KeyEvent.VK_DOWN){vorku.println(".alla");} } public void keyReleased(KeyEvent e){} public void keyTyped(KeyEvent e){} public static void main(String[] arg){ Frame f=new Frame("Jalutustoa klient"); f.add(new JalutustoaKlient()); f.setSize(400, 800); f.setVisible(true); } }