import java.applet.*; import java.awt.*; import java.io.*; import java.net.*; import java.util.*; public class Tud1 extends Applet implements Runnable { private String serverName = "193.40.238.40"; private String chatChannel = "Default"; private int serverPort=3025; private String ise="1", sk="a", omanimi=""; // Panel naod1, naod2, keskel, all, mina, ulemine; Socket socket; boolean go = true; DataOutputStream outputs; DataInputStream inputs; Thread chatConnectionThread; Pa3 tahvel1, tahvel2; Label[] nimi=new Label[5]; TextArea[] t=new TextArea[5]; Panel[] isik=new Panel[5]; Tap1 sisse=new Tap1(); Panel nupud=new Panel(); Panel pildinupud=new Panel(); Button saadatekst=new Button("Send text"); Button saadapilt=new Button("Send picture"); Button logout=new Button("Log Out"); Panel saatekast=new Panel(); Panel tekstid=new Panel(), pildid=new Panel(); Nimedialoog1 nd=new Nimedialoog1("Name input", this); public void init() { int i; for(i=0; i<5; i++){ nimi[i]=new Label(" *** "); t[i]=new TextArea(5, 30); isik[i]=new Panel(); isik[i].setLayout(new BorderLayout()); isik[i].add("Center", t[i]); isik[i].add("South", nimi[i]); t[i].setEditable(false); t[i].setBackground(new Color(235, 235, 235)); } setLayout(new BorderLayout()); tekstid.setLayout(new GridLayout(3, 2)); for(i=1; i<5; i++)tekstid.add(isik[i]); tekstid.add(isik[0]); //pildinupud.setLayout(new GridLayout(1, 2)); //pildinupud.add(ules); //pildinupud.add(alla); nupud.setLayout(new GridLayout(1, 3)); nupud.add(saadatekst); nupud.add(saadapilt); nupud.add(logout); saatekast.setLayout(new BorderLayout()); saatekast.add("Center", sisse); saatekast.add("South", nupud); tekstid.add(saatekast); add("Center", tekstid); pildid.add(tahvel2=new Pa3(this)); tahvel1=new Pa3(this); pildid.setLayout(new GridLayout(1, 2)); pildid.add(tahvel1); add("South", pildid); tahvel1.butt.setLabel("<"); tahvel2.butt.setLabel(">"); //nd.show(); chatConnectionThread = new Thread(this); chatConnectionThread.start(); } public void start(){ tahvel1.a.repaint(); //nd.show(); } public void stop(){go=false;} public void koik(){ for(int i=1; i<5; i++)isik[i].show(); sk="a"; } public void uksi(){ for(int i=1; i<5; i++)isik[i].hide(); isik[Integer.valueOf(ise.substring(0, 1)).intValue()].show(); sk="0"; } public boolean action(Event ev, Object ob) { //System.out.println("Sees"); //t[1].appendText(String.valueOf(Runtime.getRuntime().freeMemory())); if (ev.target.equals(saadatekst)) { sisse.korrasta(); StringTokenizer message =new StringTokenizer(sisse.getText(), "\n"); while(message.hasMoreTokens()){ write("t"+ise+sk+"o"+message.nextToken());} sisse.setText(""); sisse.requestFocus(); return true; } if (ev.target.equals(saadapilt)) { tahvel1.a.joonista_string(nimi[Integer.valueOf(ise).intValue()].getText(), 5, 198); synchronized(tahvel1.a){ write("p"+ise+sk+"o"+tahvel1.a.pildistring()); } } if (ev.target.equals(logout))write("k"+ise+ise+"x"); if (ev.target.equals(tahvel1.butt))tahvel2.stringipilt(tahvel1.pildistring()); if (ev.target.equals(tahvel2.butt))tahvel1.stringipilt(tahvel2.pildistring()); if(ev.target==nd.nupp){omanimi=nd.tekst.getText(); t[1].appendText(omanimi); write("k"+ise+"an"+omanimi); } return super.action(ev, ob); } public void seaise(String s){ this.ise=s; } public void run() { String message; try { socket = new Socket(serverName,serverPort); outputs = new DataOutputStream(socket.getOutputStream()); inputs = new DataInputStream(socket.getInputStream()); }catch(Exception e){System.out.println("algjama");} this.write("ktan "); while (go) { try{ message = inputs.readLine(); if ( message == null ) { go = false; System.out.println("go on false"); } else { // t[1].appendText(message+"\n"); if(message.charAt(0)=='k'){ if(message.substring(1, 3).compareTo("nr")==0){ seaise(message.substring(3, 4));} if(message.charAt(3)=='a'){koik();} if(message.charAt(3)=='u'){uksi();} if(message.charAt(3)=='n'){ if(message.length()>4) nimi[Integer.valueOf(message.substring(1, 2)).intValue()].setText( message.substring(4));} if((message.charAt(3)=='x')&(message.length()>3)) nimi[Integer.valueOf(message.substring(1, 2)).intValue()].setText(""); } if((message.charAt(0)=='t')&(message.length()>4)){ message=message+"\n"; t[Integer.valueOf(message.substring(1, 2)).intValue()].appendText(message.substring(4)); } try{ if(message.charAt(0)=='p'){tahvel1.a.stringipilt(message.substring(4));} } catch(Exception e){} } }catch (Exception e) { t[1].appendText("CONNECTION ERROR :"+"\n"); t[1].appendText(e.getMessage()+"\n");} } } void write( String mess ) { try { outputs.writeBytes(mess+"\n"); outputs.flush (); System.out.println(mess); } catch (Exception e) { t[1].appendText("OUTPUT ERROR :"+"\n"); t[1].appendText(e.getMessage()+"\n"); } } }