codice:
package clientserverprova;
import java.io.*;
import java.awt.*;
import java.net.*;
//import prominence.msg.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ServizioCinema extends Frame {
protected static int id = 0;
Label LMsgServer, LInfoFilm, LSala, LOrario, LPostiLiberi;
TextArea TAInfoFilm, TAMsgServer;
TextField TFSala, TFOrario, TFPostiLiberi;
Button BRichiedi, BPostiLiberi, BCompra, BReset;
MessageOutputStream Mos;
MessageInputStream Mis;
ObjectInputStream Ois;
ObjectOutputStream Oos;
Button [][] Mb;
String S, O;
short [][] D;
Color g, r, b;
public ServizioCinema (InputStream i, OutputStream o) throws IOException {
super ("Servizio Cinema " + (id ++));
this.setLayout(null); //che fa????
setResizable(false);
try {
Mos = new MessageOutputStream (o);
Mis = new MessageInputStream (i);
Ois = new ObjectInputStream (i);
Oos = new ObjectOutputStream (o); /* QUI SI BLOCCA TUTTO E MI FA CASINI, NON RIESCO A CAPIRE PERCHE' */
}catch (IOException e){
System.out.println("oh 4");
System.out.println("IOException E" + "\n");
}
g = Color.GREEN;
r = Color.RED;
b = Color.BLUE;
// setLayout (new GridLayout (2, 1)); che fa????
resize(570, 700);//controllare
int x = 50, y = 200;
Mb = new Button [15][10];
for (int z = 0; z < 15; z++){
for (int c = 0; c < 10; c++){
Mb[z][c] = new Button (Integer.toString(z)+Integer.toString(c));
Mb[z][c].reshape(x, y, 25, 18);
Mb[z][c].hide();
this.add(Mb[z][c]);
x = x + 30;
}
y = y + 20;
x = 50;
}
LMsgServer = new Label("Messaggi dal server");
LMsgServer.reshape(20, 520, 200, 15);
this.add(LMsgServer);
LInfoFilm = new Label("Info programmazione film sale e orari");
LInfoFilm.reshape(20, 40, 250, 15);
this.add(LInfoFilm);
LSala = new Label("Sala");
LSala.reshape(440, 175, 100, 15);
this.add(LSala);
LOrario = new Label("Orario");
LOrario.reshape(440, 230, 100, 15);
this.add(LOrario);
LPostiLiberi = new Label("Posti Liberi");
LPostiLiberi.reshape(440, 334, 100, 15);
this.add(LPostiLiberi);
TFSala = new TextField();
TFSala.reshape(440, 200, 100, 20);
this.add(TFSala);
TFOrario = new TextField();
TFOrario.reshape(440, 255, 100, 20);
this.add(TFOrario);
TFPostiLiberi = new TextField();
TFPostiLiberi.reshape(440, 360, 100, 20);
this.add(TFPostiLiberi);
TAInfoFilm = new TextArea();
TAInfoFilm.reshape(20, 60, 400, 100);
this.add(TAInfoFilm);
TAMsgServer = new TextArea();
TAMsgServer.reshape(20, 550, 500, 120);
this.add(TAMsgServer);
BRichiedi = new Button("Info Film");
BRichiedi.reshape(440, 70, 80, 50);
this.add(BRichiedi);
BPostiLiberi = new Button("Posti liberi");
BPostiLiberi.reshape(440, 280, 80, 40);
this.add(BPostiLiberi);
BCompra = new Button("Compra");
BCompra.reshape(440, 400, 80, 80);
this.add(BCompra);
BCompra.hide();
BReset = new Button("Reset");
BReset.reshape(370, 430, 50, 30);
this.add(BReset);
show(); ////////////////////////
//rinominiamo i bottoni della matrice con i e j e renderli invisibili
//il bottone compra nn è visibile, diventa visibile dopo aver schiacciato posti liberi
}
@Override
public boolean handleEvent(Event event) {
g = Color.GREEN;
r = Color.RED;
b = Color.BLUE;
if (event.id == Event.WINDOW_DESTROY) {
hide(); // hide the Frame
dispose(); // tell windowing system to free resources
System.exit(0); // exit
return true;
}
if (event.target == BRichiedi && event.id == Event.ACTION_EVENT) {
BRichiedi_Clicked(event);
}
else if (event.target == BPostiLiberi && event.id == Event.ACTION_EVENT) {
BPostiLiberi_Clicked(event);
}
else if (event.target == BCompra && event.id == Event.ACTION_EVENT) {
try {
BCompra_Clicked(event);
} catch (IOException ex) {
Logger.getLogger(ServizioCinema.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(ServizioCinema.class.getName()).log(Level.SEVERE, null, ex);
}
}
else if (event.target == BReset && event.id == Event.ACTION_EVENT) {
BReset_Clicked(event);
}
else
for (int i = 0; i < 15; i++)
for (int j = 0; i < 15; i++)
if (event.target == Mb[i][j] && event.id == Event.ACTION_EVENT
&& Mb[i][j].getForeground() == g ) {
buttonM_Clicked(i, j, event);
}
return super.handleEvent(event);
}
//--------------------------------------------------
void BRichiedi_Clicked(Event event) {
try{
Mos.writeUTF("Richiedi");
Mos.send();
}catch (IOException e){
System.out.println("Errore nella richiesta delle informazioni" + "\n");
}
TAInfoFilm.setText(""); //per cancellare il testo
try{
Mis.receive();
TAInfoFilm.append(Mis.readUTF() + "\n");
}catch (IOException e){
System.out.println("Errore nella richiesta delle informazioni" + "\n");
}
}
//--------------------------------------------------
void BPostiLiberi_Clicked(Event event) {
try {
short[][] temp;
temp = new short[15][10];
String s;
int n;
S = TFSala.getText();
O = TFOrario.getText();
//gestire try catch
Mos.writeUTF("PostiLiberi");
Mos.send();
Mos.writeUTF(S);
Mos.writeUTF(O);
Mos.send();
Mis.receive();
n = Mis.readInt();
if (n == -1) {
System.out.println("Attenzione!! Il nome della sala o l'orario è inesistente!");
} else {
temp = (short[][]) Ois.readObject(); ///////// object
TFPostiLiberi.setText(Integer.toString(n));
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 15; j++) {
if (temp[i][j] == 0) {
Mb[i][j].setForeground(g);
Mb[i][j].show();
} else if (temp[i][j] == 1) {
Mb[i][j].setForeground(r);
}
Mb[i][j].show();
/* else if (temp[i][j] == 2)
Mb[i][j].hide(); nn serve perchè è già nascosto di default*/
}
}
BCompra.hide();
}
} catch (IOException ex) {
Logger.getLogger(ServizioCinema.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(ServizioCinema.class.getName()).log(Level.SEVERE, null, ex);
}
}
//--------------------------------------------------
void BCompra_Clicked(Event event) throws IOException, ClassNotFoundException {
try{
Mos.writeUTF("Compra"); //invio il comando
Mos.send();
Mos.writeUTF(S);
Mos.writeUTF(O);
Mos.send();
}catch (IOException e) {
System.out.println("IO Exception" + "\n");
}
Oos.writeObject(D);/////////////////////OBJECT
Mis.receive();
int check = Mis.readInt();
if (check == 1 )//fallimento posti già acquistati
TAMsgServer.append(Mis.readUTF() + "\n");
else{ //check == 0
TFPostiLiberi.setText(Integer.toString(Mis.readInt()) + "\n");
TAMsgServer.append(Mis.readUTF() + "\n");
BReset_Clicked(event);
}
}
//--------------------------------------------------
void BReset_Clicked(Event event) {
b = Color.BLUE;
g = Color.green;
for (int i = 0; i < 15; i++)
for (int j = 0; i < 15; i++)
D[i][j] = 0;
BCompra.hide();
for (int i = 0; i < 10; i++)
for (int j = 0; j < 15; j++){
if (Mb[i][j].getForeground() == b ){
Mb[i][j].setForeground(g);
Mb[i][j].show();
}
}
}
//--------------------------------------------------
void buttonM_Clicked(int l, int m, Event event) {
b = Color.BLUE;
Mb[l][m].setForeground(b);
D[l][m] = 1;
BCompra.show();
}
static public void main (String args[]) throws IOException {
Socket socket = new Socket("localhost", 1414);
new ServizioCinema (socket.getInputStream(),socket.getOutputStream()).show();
}
}