//Andrus Epkin, Tarvo Tromp, Marge Nurme, Mait Kelindeman. 11.12.2000 //JUTUTOA KLIENT VERSIOON 1.0 import java.applet.Applet; import java.awt.*; import java.awt.Label; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.*; import java.net.*; public class chat2 extends Applet implements ActionListener{ TextField username=new TextField(""); Button ok=new Button("Enter"); Label silt1=new Label(); //Silt, kuhu ilmub serveri poolt saadetud tekst. //Teise osa väljad TextField tekst=new TextField("kirjuta siia"); //Tekstiväli TextArea jutt=new TextArea("",22,19, TextArea.SCROLLBARS_VERTICAL_ONLY); //Tekstiväli, kuhu ilmub sisestatud tekst Label[] sildid=new Label[50]; //Sildid, kuhu peaksid ilmuma hetkel jutukas olevate kasutajate nimed. int nimedearv=0; Socket sc; PrintWriter kasutaja; PrintWriter valja; public chat2(){ setLayout(new BorderLayout()); Panel p1=new Panel(new BorderLayout()); Panel enterchat=new Panel(new GridLayout(5,3)); //Siin joonistatakse esimene aken enterchat.add(new Panel()); enterchat.add(new Panel()); enterchat.add(new Panel()); enterchat.add(new Panel()); enterchat.add(new Panel()); enterchat.add(new Panel()); enterchat.add(new Panel()); Panel p2=new Panel(new BorderLayout()); p2.add(silt1, BorderLayout.NORTH); p2.add(username, BorderLayout.CENTER); p2.add(ok, BorderLayout.SOUTH); enterchat.add(p2); add(enterchat); enterchat.add(new Panel()); enterchat.add(new Panel()); enterchat.add(new Panel()); enterchat.add(new Panel()); enterchat.add(new Panel()); enterchat.add(new Panel()); enterchat.add(new Panel()); ok.addActionListener(this); try{ //Siin üritatakse luua ühendus serveriga ja omistada pistikule nimi sc=new Socket("127.0.0.1", 3001); kasutaja=new PrintWriter(sc.getOutputStream(), true); new ruumiloim(sc, jutt, this); }catch(Exception probleem){ System.out.println(probleem); } } public void actionPerformed(ActionEvent e){ //Tegevus, mis toimub kui on vajutatud "Enter" klahvi klaviatuuril enterit if(e.getSource()==ok){ kasutaja.println(username.getText()); System.out.println("Username sisestatud"); }else{ kasutaja.println(tekst.getText()); tekst.setText(""); System.out.println("Done!"); } } public void teine(){ setLayout(new BorderLayout()); //Siin kujundatakse teine aken. Panel p3=new Panel(new BorderLayout()); p3.add(jutt, BorderLayout.NORTH); p3.add(tekst, BorderLayout.CENTER); add(p3, BorderLayout.CENTER); Panel p4=new Panel(new BorderLayout()); tekst.addActionListener(this); Panel nimed=new Panel(new GridLayout(20,1)); for(int x=0;x<15;x=x+1){ nimed.add(sildid[x]=new Label("")); p4.add(nimed); }; ScrollPane sp=new ScrollPane(); sp.add(p4); add(sp, BorderLayout.EAST); validate(); } public static void main(String argumendid[]){ //Siin joonistatakse freimid Frame f=new Frame(); f.add(new chat2()); f.setSize(500,400); f.setVisible(true); f.addWindowListener( new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } } ); } class ruumiloim extends Thread{ Socket sc; TextArea ta; chat2 peremees; public ruumiloim(Socket sc1, TextArea ta1,chat2 pe1){ sc=sc1; ta=ta1; peremees=pe1; start(); } public void run(){ try{ BufferedReader sisse=new BufferedReader( //Kasutajanime sisselugemine new InputStreamReader(sc.getInputStream()) ); String rida=null; do{ rida=sisse.readLine(); //Loetakse rida sisse peremees.silt1.setText(rida); //Silt1-le pannakse sisse loetud rea tekst. if (silt1.getText().equals("Selline nimi on olemas!")){ System.out.println(silt1); Thread.sleep(1000); peremees.silt1.setText("Proovi uuesti"); }else{ removeAll(); teine(); } }while(silt1.getText().equals("Proovi uuesti")); while(rida!=null){ ta.setText(ta.getText()+"\n"+rida); ta.setCaretPosition(ta.getText().length()); rida=sisse.readLine(); } }catch(Exception viga){ System.out.println(viga); } } } }