Questa, ad esempio, è una bozza di un'applet che effettua l'autenticazione presso un server, prima di cominciare a visualizzare delle immagini, che il server stesso le invia. Puoi cominciare da questa... la parte server non la posto, per non dilungare inutilmente il post
codice:
import java.io.*;
import java.net.*;
import javax.swing.*;
import java.applet.*;
import java.awt.event.*;
public class Client extends JApplet implements ActionListener {
private class Gestione extends Thread {
private ObjectInputStream ois;
public Gestione(InputStream is) {
try {
ois = new ObjectInputStream( is );
} catch (Exception e) { e.printStackTrace(); }
}
public void run() {
String str = null;
int[] pos = null;
try {
Messaggio msg = (Messaggio) ois.readObject();
if (msg.getId() == Messaggio.ACCEPT) {
while( true ) {
msg = (Messaggio) ois.readObject();
switch( msg.getId() ) {
// ... tutti i casi previsti per le azioni inviate dal server
}
}
} else {
lbl.setText("Autenticazione fallita!");
}
} catch (Exception e) { e.printStackTrace(); }
}
}
private JPanel jpLogin;
private JLabel lblLogin;
private JLabel lblPasswd;
private JTextField txtLogin;
private JPasswordField txtPwd;
private JButton jbLogin;
private JLabel lbl;
private ImageIcon img;
private Socket s;
private Gestione ges;
private String strLogin;
public void init() {
getContentPane().setLayout( null );
creaPannelloLogin();
validate();
}
private void creaPannelloLogin() {
lblLogin = new JLabel("Nome utente:");
lblLogin.setBounds(20, 20, 130, 20);
lblLogin.setHorizontalAlignment( JLabel.RIGHT );
lblPasswd = new JLabel("Password:");
lblPasswd.setBounds(20, 50, 130, 20);
lblPasswd.setHorizontalAlignment( JLabel.RIGHT );
txtLogin = new JTextField();
txtLogin.setBounds(160, 20, 200, 20);
txtPwd = new JPasswordField();
txtPwd.setBounds(160, 50, 200, 20);
jbLogin = new JButton("Accedi");
jbLogin.setBounds(280, 80, 80, 25);
jbLogin.addActionListener( this );
jpLogin = new JPanel();
jpLogin.setLayout( null );
jpLogin.setBounds(140, 200, 400, 200);
jpLogin.add( lblLogin );
jpLogin.add( lblPasswd );
jpLogin.add( txtLogin );
jpLogin.add( txtPwd );
jpLogin.add( jbLogin );
getContentPane().add( jpLogin );
}
public void actionPerformed(ActionEvent ae) {
strLogin = ... // Compongo la stringa di autenticazione
getContentPane().remove( jpLogin );
validate();
lbl = new JLabel( "Attendere. Connessione in corso..." );
lbl.setHorizontalAlignment(JLabel.CENTER);
lbl.setBounds(10, 10, 790, 590);
getContentPane().add( lbl );
validate();
try {
connetti();
} catch (Exception e) {
resize(200, 200);
if (lbl != null) lbl.setText("Connessione con il server non riuscita.");
e.printStackTrace();
}
}
private void connetti() throws Exception {
s = new Socket(getCodeBase().getHost(), 2785);
lbl.setText("Connesso al server remoto. Sincronizzazione dati...");
ObjectOutputStream oos = new ObjectOutputStream( s.getOutputStream() );
oos.writeObject( strLogin );
oos.flush();
ges = new Gestione( s.getInputStream() );
ges.start();
}
private void cambiaImmagine(String s) {
lbl.setText("");
img = new ImageIcon( getImage(getCodeBase(), "./imgs/" + s) );
lbl.setIcon( img );
}
}
Ciao.