import java.awt.*;
import java.awt.geom.*;

/**
 * Graphe de la ligne 21 du CCD
 * @author Delphine Dard, Aout 2001
 */
public class CCDGraphe extends Canvas{
    /** La hauteur du graphe */
    static public int hauteurGraphe = 200;
    /** La largeur du graphe  */
    static public int largeurGraphe = 41*4;
    /**Distance depuis le bord en X */
    static public int bordX = 60;
    /**Distance depuis le bord en Y */
    static public int bordY = 30;

    /** La valeur maximum atteinte */
    public int maximum;
    /** L'echelle de la hauteur */
    public int echelle;
    /** l'extremite des abscisses */
    public int extremiteX = largeurGraphe+bordX;
    /**La hauteur des ordonnees */
    public int coordonneeOx = hauteurGraphe+bordY;

    /** Tableau des valeurs d'intensites */
    public int[] intensites;

    /** image pour le doubel-buffer */
    public Image buffer;

    /**
     * Constructeur
     */
    public CCDGraphe(){
	super();
	this.intensites = null;
	setSize(275, coordonneeOx+40);
	Dimension d = getSize();
	buffer = createImage(d.width,d.height);
    }

    /**
     * Constructeur avec le tableau d'intensites
     */
    public CCDGraphe(int[] intensites){
	this();
	this.intensites = intensites;
    }

    /**
     * reecrit update pour le double-buffer
     */
    public void update(Graphics g){
	paint(g);
    }

    /**
     * Dessine dans l'image puis l'affiche une fois termine
     * @param g le graphique a dessiner
     */
    public void paint(Graphics g){
	Dimension d = getSize();
	if (buffer == null)
	    buffer = createImage(d.width, d.height);
	Graphics bufferGraphic = buffer.getGraphics();

	maximum();
	bufferGraphic.setColor(Color.white);
	bufferGraphic.clearRect(0, 0, d.width, d.height);

	bufferGraphic.setColor(Color.black);
	ordonnees(bufferGraphic);
	traceIntensite(bufferGraphic);

	g.drawImage(buffer,0,0,this);
    }

    /**
     * Calcul l'intensite maximum possible
     */
    public void maximum() {
	maximum = intensites[0];
	for (int i=1; i<intensites.length; i++) {
	    if (intensites[i] > maximum)
		maximum = intensites[i];
	}
    }

    /**
     * Dessine les axes
     * @param g le graphique a dessiner
     */
    public void ordonnees(Graphics g) {	
	g.drawRect(bordX,  bordY, largeurGraphe, hauteurGraphe);
	g.drawString("Intensit\u00e9", 10, bordY-15);
	g.drawString("Pixels", bordX+60, coordonneeOx+30);
	int i = 0;
	while ((i*4) < largeurGraphe) {
	    int x = i*4+bordX;
	    g.drawLine(x, coordonneeOx+5, x, coordonneeOx-5);
	    String string = String.valueOf(i);
	    if (string.length() == 1)
		string = " " + string;
	    g.drawString(string,
			 x-7,
			 coordonneeOx+15);
	    i = i+10;
	}

	int j = 0;
	int pas = 50;
	echelle = maximum/200+2;
	while (j <= 200) {
	    int y = coordonneeOx - j;
	    g.drawLine(bordX-5, y, bordX+5, y);
	    String string = String.valueOf(j*echelle);
	    if (string.length() == 4)
		string = "  " + string;
	    if (string.length() == 3)
		string = "    " + string;
	    if (string.length() == 2)
		string = "      " + string;
	    if (string.length() == 1)
		string = "        " + string;
	    g.drawString(string,
			 10,
			 y+5);
	    j = j+pas;
	}

    }


    /**
     * Dessine le graphe des intensites
     * @param g le graphique a dessiner
     */
    public void traceIntensite(Graphics g){
	g.setColor(Color.blue);
	Graphics2D g2 = (Graphics2D)g;
	for (int i=1 ; i < intensites.length; i++){
	    Line2D.Double line = new Line2D.Double(i*4+bordX, 
						   coordonneeOx-intensites[i-1]/echelle,
						   (i+1)*4+bordX, 
						   coordonneeOx-intensites[i]/echelle);
	    g2.draw(line);
	}
    }

    /**
     * Met un nouveau tableau d'intensites
     */
    public void setNewGraphe(int[] intensites){
	this.intensites = intensites;
    }
}
