/** FILE: grafico.java **/

package SIMULA;

import BUFFER.accex;
import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;

// Referenced classes of package SIMULA:
//            simulator

/*
 * CLASS grafico
 *
 * visualizza i tempi di esecuzione di tutti i processi e calcola il tempo medio
 * stampa la tabella del grafico su i file mittenti.txt o destinatari.txt
 */

class grafico extends Frame
{


    /*
     * COSTRUTTORE
     * 
     * stampa su file e calcola il tempo medio di attesa
     *
     */

    grafico(accex accex1, String s)
    {
        color = (new Color[] {
            Color.blue, Color.red, Color.green, Color.cyan, Color.pink, Color.darkGray, Color.orange, Color.black, Color.magenta, Color.yellow});
        entro = true;
        Max = 1;
        int i = 0;
        int j = 0;
        try
        {
            FileOutputStream fileoutputstream = new FileOutputStream(s + ".txt");
            for(accex accex2 = accex1; accex2.next != null; accex2 = accex2.next)
            {
                if(Max < accex2.acc2 - accex2.acc1)
                    Max = accex2.acc2 - accex2.acc1;
                i++;
                fileoutputstream.write(("ENTRATA = " + incolonna(accex2.acc1) + "\t\t").getBytes());
                fileoutputstream.write(("USCITA = " + incolonna(accex2.acc2) + "\t\t").getBytes());
                fileoutputstream.write(("TEMPO di ESECUZIONE = " + incolonna(accex2.acc2 - accex2.acc1) + "\n").getBytes());
                j += accex2.acc2 - accex2.acc1;
            }

            tempomedio = (double)j / (double)i;
            fileoutputstream.write(("\nTOTALE TEMPI DI ESECUZIONE = " + j + "\n").getBytes());
            fileoutputstream.write(("NUMERO DI PROCESSI = " + i + "\n\nTEMPO MEDIO DI ESECUZIONE = ").getBytes());
            fileoutputstream.write((tempomedio + "\nTEMPO MEDIO DI ATTESA = " + (tempomedio - 1.0D)).getBytes());
            fileoutputstream.close();
        }
        catch(IOException _ex) { }
        myvett = new int[i];
        for(int k = 0; k < i; k++)
        {
            myvett[k] = accex1.acc2 - accex1.acc1;
            accex1 = accex1.next;
        }

        if(s == "Mittenti")
            simulator.maxput = Max;
        else
            simulator.maxget = Max;
        dis = 476 / myvett.length;
        alt = 680 / Max;
        if(dis == 0)
            dis = 1;
        if(alt == 0)
            alt = 1;
        larghezza = dis * myvett.length;
        altezza = alt * Max;
        if(alt > 226)
        {
            alt = 226;
            altezza = alt * 3;
        }
        resize(35 + larghezza, altezza + 25);
        titolo = s;
        setTitle(s);
        if(!simulator.nograp)
            show();
    }

    /*
     * METODO incolonna()
     * 
     * incolonna i numeri interi
     *
     */

    String incolonna(int i)
    {
        if(i > 99)
            return String.valueOf(i);
        if(i > 9)
            return " " + i;
        else
            return "  " + i;
    }

     /*
     * METODO quante()
     * 
     * restituisce la larghezza in pixel di rettangolo del grafico
     *
     */

    private int quante()
        throws ArithmeticException
    {
        int i = 10 / alt;
        if(i == 0)
            i = 1;
        return i;
    }



    /*
     * METODO paint()
     * 
     * stampa sullo schermo il grafico
     *
     */

    public void paint(Graphics g)
    {
        if(entro)
        {
            if(titolo == "Mittenti")
                setBounds(0, 0, 35 + larghezza, altezza + 25);
            else
                setBounds(35 + larghezza, 0, 35 + larghezza, altezza + 25);
            entro = false;
        }
        col = 0;
        int i = 30 - dis;
        int j = quante();
        for(int k = 0; 28 + altezza > k * alt; k += j)
        {
            if(k != 0)
                g.drawString(incolonna(k), 10, (28 + altezza) - k * alt);
            g.drawLine(30, (23 + altezza) - k * alt, larghezza + 35, (23 + altezza) - k * alt);
        }

        for(int l = 0; l < myvett.length; l++)
        {
            g.setColor(color[col++ % 10]);
            g.fillRect(i += dis, (23 + altezza) - myvett[l] * alt, dis, myvett[l] * alt);
        }

        g.setColor(Color.blue);
        int i1 = (int)(tempomedio * (double)alt);
        g.fillRect(30, (21 + altezza) - i1, larghezza + 35, 4);
        g.drawString("Tempo medio", larghezza - 70, (18 + altezza) - i1);
    }


    /*
     * METODO handleEvent(Event event) //RIDEFINIZIONE
     * 
     * controlla gli eventi che succedono nelle finestra
     *
     */

    public boolean handleEvent(Event event)
    {
        if(event.id == 201) // se si preme X
        {
            simulator.chiudi++;
            hide();         // chiude la finestra
        }
        return super.handleEvent(event);
    }


    /*
     * VARIABILI
     * 
     */

    Color color[];
    int myvett[];
    int Max;
    int col;
    int dis;
    int alt;
    int altezza;
    int larghezza;
    double tempomedio;
    String titolo;
    boolean entro;
}