import java.io.*; import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.util.*; import java.net.*; public class Jutukas extends Applet implements ActionListener, Runnable { boolean olentrue = true; boolean onyhendus = false; String sisend; PrintWriter valja; BufferedReader sisse; Button nupp = new Button("Ühenda"); TextField IP = new TextField(); Socket sc; //Jutuka jaox TextArea jutukaText = new TextArea("",20,5,1); TextField jutukaSonum = new TextField(); public Jutukas() //konstruktor { Panel ylaOsa = new Panel(new GridLayout(1,2)); //paneme kujunduse paika ylaOsa.add(IP); ylaOsa.add(nupp); nupp.addActionListener(this); Panel jutukaOsa = new Panel(new BorderLayout()); jutukaOsa.add(jutukaText, BorderLayout.NORTH); jutukaOsa.add(jutukaSonum, BorderLayout.SOUTH); jutukaSonum.addActionListener(this); Panel suurPaneel = new Panel(new BorderLayout()); suurPaneel.add(ylaOsa, BorderLayout.NORTH); suurPaneel.add(jutukaOsa, BorderLayout.SOUTH); setLayout(new BorderLayout()); add(suurPaneel, BorderLayout.NORTH); //jutukaText.setEnabled(false); //jutukaSonum.setEnabled(false); võta ära new Thread(this).start(); } public void run() //see vinge lõime värk { try { if (olentrue == true) { ServerSocket ss = new ServerSocket(3001); Socket sc = ss.accept(); sisse = new BufferedReader(new InputStreamReader(sc.getInputStream())); valja = new PrintWriter(sc.getOutputStream(), true); nupp.setLabel("Katkesta"); IP.setEnabled(false); } while(true) //ootame ja vaatame, mis inffi tuleb { sisend = sisse.readLine(); nupp.setLabel("Katkesta"); IP.setEnabled(false); jutukaSonum.setEnabled(true); jutukaText.append("Vastane: " + sisend + "\n"); } } catch (Exception e){System.out.println("Probleem: "+e);} } public void actionPerformed(ActionEvent e) //kui klikataxe ühenduse nuppu ja hakataxe kliendina tööle { String ipAadress = IP.getText(); if(e.getSource()==nupp) { try { olentrue = false; Socket sc = new Socket (ipAadress, 3001); sisse = new BufferedReader(new InputStreamReader(sc.getInputStream())); valja = new PrintWriter(sc.getOutputStream(), true); nupp.setLabel("Katkesta"); IP.setEnabled(false); jutukaSonum.setEnabled(true); new Thread(this).start(); }catch(Exception er){System.out.println("Probleem");} if (onyhendus==true) { try { valja.close(); sisse.close(); nupp.setLabel("Ühenda"); } catch (Exception efas){System.out.println("suur jama");} } } if (e.getSource() == jutukaSonum) //saadan teate teisele { jutukaText.append("Mina: " + jutukaSonum.getText() + "\n"); valja.println(Beibesta(jutukaSonum.getText())); jutukaSonum.setText(""); } } public String Beibesta(String transText) { String abi = ""; transText = transText + " "; for (int i=0; i0.9) return " camooon "; else if (teen>0.8) return " äääää "; else if(teen>0.7) return " :D:D:D "; else if (teen>0.6) return " eeeee..(A) "; else if(teen>0.5) return " ää..uu "; else if(teen>0.4) return " :D:D:D:D:D "; else if(teen>0.3) return " iccc "; return " "; } public static void main(String argumendid[]) { Frame f = new Frame("Jutukas :)"); f.setSize(410,450); f.add(new Jutukas()); f.setVisible(true); } };