codice:
import java.awt.*;
import java.util.*;
import java.io.*;
import java.net.*;
import java.applet.Applet;
import javax.sound.midi.*;
//un thread esegue la applet
//la quale parla con un passacarte(spam e smaz?)
//il quale parla con il server
//questa logica è dovuta a un misterioso baco malefico di java....
public class Test extends Applet { // il client è un applet, viene chiamato da un file html
final static String HOST = "localhost";
final static int PORT = 54321;
static int MINALT = 128;
static int MAXALT = 128;
static int MINDUR = 128;
static int MAXDUR = 128;
static boolean ON = false;
Scrollbar minaltSlider,
maxaltSlider,
mindurSlider,
maxdurSlider;
Button playerButton;
Synthesizer synt;
Receiver recv;
public static void main (String args[]) {
try {
ServerSocket w = new ServerSocket(Test.PORT);
(new Span(w.accept())).start();
} catch (Exception e) {
System.out.println("Exception in Test: " + e);
e.printStackTrace();
}
}
public void init() {// è la funzione che fa avviare le applet
setLayout(new BorderLayout(15,15)); //utilizza il secondo metodo costruttore della clase borderlayout, quello con in ingresso i margini ra un elemento e l'altro(in questo caso di 15x15)
this.playerButton = new Button("Play");
this.minaltSlider = new Scrollbar(Scrollbar.VERTICAL, Test.MINALT, 1, 0, 128);
this.maxaltSlider = new Scrollbar(Scrollbar.VERTICAL, Test.MAXALT, 1, 0, 128);
this.mindurSlider = new Scrollbar(Scrollbar.HORIZONTAL, Test.MINDUR, 1, 1, 101);
this.maxdurSlider = new Scrollbar(Scrollbar.HORIZONTAL, Test.MAXDUR, 1, 1, 101);
add("West", this.minaltSlider);
add("East", this.maxaltSlider);
add("North", this.mindurSlider);
add("South", this.maxdurSlider);
add("Center", this.playerButton);
(new Play()).start();
}
//nell'evento creato possiamo avere: es, mouse clicca a coordinate x,y; handle event controlla circa 60 volte al secondo per ogni volta che il tasto del mouse è pigiato e ogni volta crea un evento.
public boolean handleEvent(Event e) { //ad ogni evento relativo a un cambiamento delle condizioni delle unità di input come:clic,spostamento cursore, tasto digitato da tastiera ecc.. viene generato un evento di tipo handle event
if (e.target.equals(minaltSlider)) {// target permette di individuare la componente alle cordinate dove abbiamo cliccato, se era un bottone una scrollbar ecc. Se quello che abbiamo cliccato è il minaltslider...
Test.MINALT = minaltSlider.getValue(); //metti nella var minalt il valore che segnala lo slider minalt
System.out.println("minal"+Test.MINALT);
if (Test.MINALT > Test.MAXALT) { //se minalt prova a superare maxalt
Test.MAXALT = Test.MINALT; // poni il MINALt=a MAXALT
maxaltSlider.setValue(Test.MAXALT);
}// se il minimo sale oltre il valore attuale del massimo, fai salire anche il massimo
return true;
}
if (e.target.equals(maxaltSlider)) {//se abbiamo cliccato lo slider di destra...
Test.MAXALT = maxaltSlider.getValue();
System.out.println("mxal"+Test.MAXALT);
if (Test.MINALT > Test.MAXALT) {
Test.MINALT = Test.MAXALT;
minaltSlider.setValue(Test.MINALT);// se con il massimo scende si porta dietro il minimo
}
return true;
}
if (e.target.equals(mindurSlider)) {
Test.MINDUR = mindurSlider.getValue();
System.out.println("mndu"+Test.MINDUR);
if (Test.MINDUR > Test.MAXDUR) {
Test.MAXDUR = Test.MINDUR;
maxdurSlider.setValue(Test.MAXDUR);
}
return true;
}
if (e.target.equals(maxdurSlider)) {
Test.MAXDUR = maxdurSlider.getValue();
System.out.println("mxdu"+Test.MAXDUR);
if (Test.MINDUR > Test.MAXDUR) {
Test.MINDUR = Test.MAXDUR;
mindurSlider.setValue(Test.MINDUR);
}
return true;
}
return super.handleEvent(e);
}
public boolean action(Event e, Object arg) {//se cliccano sul bottone
if (!Test.ON) { // e se on è false, quello diventa
Test.ON = true; //quello diventa true
((Button)(e.target)).setLabel("Stop");
return true;
}
if (Test.ON) {
Test.ON = false;
((Button)(e.target)).setLabel("Play");
return true;
}
return true;
}
}
class Note {
int alt;
int dur;
public Note(int alt, int dur) {
this.alt = alt;
this.dur = dur;
}
public void play(Receiver recv) {
try {
int timeStamp = -1;
ShortMessage dn = new ShortMessage();
ShortMessage up = new ShortMessage();
dn.setMessage(ShortMessage.NOTE_ON,0,this.alt,127);
up.setMessage(ShortMessage.NOTE_OFF,0,this.alt,127);
System.out.println("Note = (" + this.alt + "," + this.dur + ")");
recv.send(dn,timeStamp);
Thread.sleep((int)(10*this.dur));
recv.send(up,timeStamp);
} catch (Exception e) {}
}
}
class Play extends Thread { //regola i messaggi che partono dal client al server
public void run() {
try {
Synthesizer synt = MidiSystem.getSynthesizer();
if (!synt.isOpen()) synt.open();
Receiver recv = synt.getReceiver();
Socket sock = new Socket(Test.HOST, Test.PORT);
InputStream read = sock.getInputStream();
OutputStream rite = sock.getOutputStream();
boolean tempON = Test.ON;
int tempMINALT = Test.MINALT;
int tempMAXALT = Test.MAXALT;
int tempMINDUR = Test.MINDUR;
int tempMAXDUR = Test.MAXDUR;
System.out.println("Starting playing...");
while (true) {
Thread.sleep(10);
if (tempON != Test.ON) {
System.out.println("Transmitting ON/OFF");
tempON = Test.ON;
if (!Test.ON) rite.write(0); //PER IL BOTTONE ON VEDIAMO CHE IL CLIENT SCRIVE NEL CANALE 0,che nel server era codificato come on
if (Test.ON) rite.write(1);// stessa osa per 1
}
while (Test.ON) {// se on è true fai continuamnte
if (tempMINALT != Test.MINALT || tempMAXALT != Test.MAXALT) {
System.out.println("Transmitting MINALT/MAXALT");
tempMINALT = Test.MINALT;
tempMAXALT = Test.MAXALT;
rite.write(2); //se guardiamo nel server, nel case=2--> ri ricevono il minimo e il massimo di un intervallo
rite.write(tempMINALT); //queste write sono in corrispondenza con le read che avevamo nel rever
rite.write(tempMAXALT); //qui abbiamo delle variabili, ottenute da assegnamento, e poi scritte
} //possono essere quei messaggi che nel server si andavano a leggere
if (tempMINDUR != Test.MINDUR || tempMAXDUR != Test.MAXDUR) {
System.out.println("Transmitting MINDUR/MAXDUR");
tempMINDUR = Test.MINDUR;
tempMAXDUR = Test.MAXDUR;
rite.write(3);
rite.write(Test.MINDUR);
rite.write(Test.MAXDUR);
}
int alt = read.read(); //dopo aver trasmesso guardiamo cosa il server ci trasmette: si tratta di un altezza e una durata con cui creare delle note
//in sostanza: IL CLIENT modifica la applet tramite l'interaz dell'utente, manda le variazioni al server che calcola il valore casuale tra i valori provenienti dgli slider e rinvvia il tutto al client che con cuei valori fa le note.
int dur = read.read();
(new Note(alt, dur)).play(recv);
}
}
} catch (Exception e) {
System.out.println("Exception in Play: " + e);
}
}
}
//DA QUI IN POI IL CODICE SI OCCUPA SOLO DI PASSARE IN INPU I DATI,POCO SIGNIFICATIVO PER CAPIRE IL SENSO DEL CODICE
class Span extends Thread {
final static String HOST = "localhost"; //è il nome della macchina su cui andremo a installare il server
final static int PORT = 12345;
Socket loc;
Socket rem;
public Span(Socket s) {
try {
this.loc = s;
this.rem = new Socket(Span.HOST, Span.PORT);
} catch (Exception e) {
System.out.println("Exception in Span: " + e);
e.printStackTrace();
}
}
public void run() {
try {
(new Smaz(this.loc.getInputStream(), this.rem.getOutputStream())).start();
System.out.println("Spanned Smaz1");//OGNI smaz legge e trasmette numeri
(new Smaz(this.rem.getInputStream(), this.loc.getOutputStream())).start();
System.out.println("Spanned Smaz2");
} catch (Exception e) {
System.out.println("Exception in Span: " + e);
e.printStackTrace();
}
}
}
class Smaz extends Thread {
InputStream read;
OutputStream rite;
public Smaz(InputStream r, OutputStream w) {
this.read = r;
this.rite = w;
}
public void run() {
try {
int b;
while ((b = this.read.read()) != -1) {
this.rite.write(b);
}
} catch (Exception e) {
System.out.println("Exception in Smaz: " + e);
e.printStackTrace();
}
}
}
//TEMA D'ESAME:
//CONSIDERANDO il centro: se con un cursore ho, al centro, il do 3, voglio che se il cursore di sinistra e 30 passi sotto
//il do tre, quello di destra dev'essere 30 passi sopra. insomma deve muoversi specularmente.
//ci sarà da lavorare (quasi) solamente: verrà chiesto di modificare l'interfaccia utente
//nell'evento creato possiamo avere: es, mouse clicca a coordinate x,y
/* public boolean handleEvent(Event e) { //ad ogni evento relativo a un cambiamento delle condizioni delle unità di input.clic,spostamento cursore, tasto digitato da tastiera ecc.. viene generato un evento di tipo handle event
if (e.target.equals(minaltSlider)) {// target permette di individuare la componente alle cordinate dove abbiamo cliccato, se era un bottone una scrollbar ecc. Se quello che abbiamo cliccato è il minalslider...
Test.MINALT = minaltSlider.getValue(); //metti nella var minalt il valore che segnala lo slider minalt
if (Test.MINALT > Test.MAXALT) { //se minalt prova a superare maxalt
Test.MAXALT = Test.MINALT; // poni il MINALt=a MAXALT
maxaltSlider.setValue(Test.MAXALT);
}
return true;
}
if (e.target.equals(maxaltSlider)) {//se abbiamo cliccato lo slider di destra...
Test.MAXALT = maxaltSlider.getValue();
if (Test.MINALT > Test.MAXALT) {
Test.MINALT = Test.MAXALT;
minaltSlider.setValue(Test.MINALT);
}
return true;
}
if (e.target.equals(mindurSlider)) {
Test.MINDUR = mindurSlider.getValue();
if (Test.MINDUR > Test.MAXDUR) {
Test.MAXDUR = Test.MINDUR;
maxdurSlider.setValue(Test.MAXDUR);
}
return true;
}
if (e.target.equals(maxdurSlider)) {
Test.MAXDUR = maxdurSlider.getValue();
if (Test.MINDUR > Test.MAXDUR) {
Test.MINDUR = Test.MAXDUR;
mindurSlider.setValue(Test.MINDUR);
}
return true;
}
return super.handleEvent(e);
}
*/