1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
| import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
/**
*
* @author pj
*/
public class AppletChat extends JApplet {
private JTextField jtf;
private JPanel bas;
private JPanel haut;
private int port;
private int idChat;
private int idUtilisateur;
private JScrollPane scroll;
private JLabel texteChat;
private JScrollBar barre;
public Timer t;
/**
* Initialization method that will be called after the applet is loaded
* into the browser.
*/
@Override
public void init() {
// TODO start asynchronous download of heavy resources
this.idChat=Integer.parseInt(getParameter("idChat"));
this.idUtilisateur=Integer.parseInt(getParameter("idUtilisateur"));
this.port=Integer.parseInt(getParameter("port"));
this.getContentPane().setBackground(new Color(0xd3d9f2));
bas=new JPanel();
haut=new JPanel();
scroll=new JScrollPane(haut);
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
scroll.setWheelScrollingEnabled(true);
jtf=new JTextField();
texteChat=new JLabel("Please wait");
/*String txt="<html><div align=\"left\" style=\"width: 600px;\">";
for(int i=0;i<50;i++)
txt+="ligne : "+i+"<br/>";
txt+="</div></html>";*/
//texte.setText(txt);
//area=new JTextArea("Please wait...",20, 50);
Font policeA=new Font("Arial", Font.BOLD, 11);
texteChat.setFont(policeA);
//area.setPreferredSize(new Dimension(600,350));
//haut.add(area, BorderLayout.CENTER);
Font police=new Font("Verdana", Font.BOLD, 12);
jtf.setFont(police);
jtf.setForeground(new Color(0x006cfb));
jtf.setPreferredSize(new Dimension(450,30));
jtf.addKeyListener(new ClavierListener());
bas.setBackground(new Color(0xc0c8e9));
haut.setBackground(new Color(0xb8b5c7));
bas.add(jtf, BorderLayout.WEST);
haut.add(texteChat);
this.getContentPane().add(bas, BorderLayout.SOUTH);
//this.getContentPane().add(haut, BorderLayout.CENTER);
this.getContentPane().add(scroll, BorderLayout.CENTER);
jtf.requestFocus();
t=new Timer();
t.schedule(new send(), 0, 5000);
}
private class ClavierListener implements KeyListener {
public void keyTyped(KeyEvent ke) {
//throw new UnsupportedOperationException("Not supported yet.");
}
public void keyPressed(KeyEvent ke) {
if(ke.getKeyCode()==KeyEvent.VK_ENTER&&jtf.getText().length()!=0) {
sendDatas s=new sendDatas();
s.start();
}
}
public void keyReleased(KeyEvent ke) {
//throw new UnsupportedOperationException("Not supported yet.");
}
}
public class sendDatas extends Thread {
@Override
public void run() {
try {
String texte = jtf.getText();
texte=idChat+"#"+idUtilisateur+"#"+texte;
jtf.setText("");
InetAddress serveur = InetAddress.getByName("localhost");
Socket socket = new Socket(serveur, port);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out =new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
out.println(texte);
String txt="";
while(true) {
String str=in.readLine();
if(str==null||str.equals("#END#"))
break;
else
txt+=str;
}
if(txt.length()>0) {
txt="<html><div align=\"left\" width=\"600\">"+txt+"</div><br/><br/><br/><br/></html>";
texteChat.setText(txt);
}
in.close();
out.close();
socket.close();
barre=scroll.getVerticalScrollBar();
barre.setValue(barre.getMaximum()+30);
} catch (IOException ex) {
Logger.getLogger(AppletChat.class.getName()).log(Level.SEVERE, null, ex);
jtf.setText("erreur fatale");
}
}
}
public class send extends TimerTask {
private Socket sock;
@Override
public void run() {
try {
InetAddress serveur = InetAddress.getByName("localhost");
sock= new Socket(serveur, port);
/*BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
/*out.println("#send#");
String txt="";
while(true) {
String str=in.readLine();
if(str==null||str.equals("#END#"))
break;
else
txt+=str;
}
if(txt.length()>0) {
txt="<html><div align=\"left\" width=\"600\">"+txt+"</div><br/><br/><br/><br/></html>";
texteChat.setText(txt);
}
in.close();
out.close();*/
//sock.close();
barre=scroll.getVerticalScrollBar();
barre.setValue(barre.getMaximum()+30);
} catch (IOException ex) {
Logger.getLogger(AppletChat.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
// TODO overwrite start(), stop() and destroy() methods |
Partager