codice:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.StringTokenizer;
public class FinestraChat extends Frame {
TextArea taNomi = new TextArea();
TextField tMessaggio = new TextField();
TextArea taChat = new TextArea();
Button button1 = new Button();
Image[] img = new Image[5];
int x,y;
TextArea path = new TextArea();
public FinestraChat(String titolo) {
this();
setTitle(titolo);
}
public FinestraChat() {
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
this.setBackground(new Color(0, 192, 0));
this.setSize(new Dimension(571, 935));
this.setTitle("Finestra di Accesso");
this.setLocation(0,0);
this.setLayout(null);
taNomi.setBackground(Color.white);
taNomi.setFont(new Font("TimesRoman", 0, 14));
taNomi.setBounds(new Rectangle(453, 29, 107, 400));
tMessaggio.setBackground(Color.white);
tMessaggio.setBounds(new Rectangle(18, 438, 533, 33));
taChat.setBackground(Color.white);
taChat.setFont(new Font("TimesRoman", 1, 14));
taChat.setBounds(new Rectangle(11, 28, 437, 401));
button1.setFont(new Font("TimesRoman", 1, 14));
button1.setBounds(new Rectangle(211, 473, 115, 43));
button1.setLabel("Invia");
button1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
this.add(taNomi, null);
this.add(tMessaggio, null);
this.add(taChat, null);
this.add(button1, null);
//this.add(path, null);
//variabile che "dovrebbe" contenere il nome del thread
String nomeThread = null;
new ThreadClient(this, nomeThread).start();
tMessaggio.requestFocus();
}
public void paint(Graphics g) {
if (img[0]!=null)
g.drawImage(img[0],40,550,img[0].getWidth(null),img[0].getHeight(null), this);
if (img[1]!=null)
g.drawImage(img[1],40,750,img[1].getWidth(null),img[1].getHeight(null), this);
}
void button1_actionPerformed(ActionEvent e) {
try {
if (!tMessaggio.getText().trim().equalsIgnoreCase("")) {
ChatClient.os.writeBytes(tMessaggio.getText()+"\n");
tMessaggio.setText("");
}
} catch (IOException ioe) {System.out.println("Errore = "+ioe);}
tMessaggio.requestFocus();
}
}
class ThreadClient extends Thread {
FinestraChat fc;
String nomeT;
ThreadClient(FinestraChat fc, String nomeT) {
this.fc = fc;
this.nomeT = nomeT;
}
public void run() {
try {
while (true) {
String str = ChatClient.is.readLine();
if (str == null) break;
if (str.substring(0,1).equalsIgnoreCase("U")) {
fc.taNomi.setText("");
StringTokenizer toke = new StringTokenizer(str.substring(1),">");
while (toke.hasMoreElements()) {
//aggiunge la nuova userId all'elenco Utenti nella Finestra CHAT
fc.taNomi.setText(fc.taNomi.getText()+toke.nextToken()+">"+"\n");
}
}
else if (str.substring(0,1).equalsIgnoreCase("T")) {
//se entriamo qui significa che si tratta di un nuovo Messaggio lasciato da un utente
fc.taChat.setText(fc.taChat.getText()+str.substring(1)+"\n");
}
else if (str.substring(0,1).equalsIgnoreCase("I")) {
//Qui mi server il WAIT del Thread
StringTokenizer toke = new StringTokenizer(str.substring(1),"-");
int i=0;
while (toke.hasMoreElements()) {
String tempTok = toke.nextToken();
System.out.println(tempTok);
fc.img[i] = Toolkit.getDefaultToolkit().createImage(tempTok.substring(0,tempTok.indexOf("_")));
i++;
}
}
}
} catch (IOException ioe) {}
super.run();
}
}