
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.JPanel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Andréa
 */
public class AffichageCamenbert extends JPanel implements Observer {

    private int[] values = new int[3];

    public AffichageCamenbert(Triplet t) {
        t.addObserver(this);
        values[0] = t.getA();
        values[1] = t.getB();
        values[2] = t.getC();
    }

    @Override
    public void paintComponent(Graphics g) {
        int startAngle = 0;
        int arcAngle;
        int cote = Math.min(getBounds().width, getBounds().height);
        int marge = (getBounds().width - cote)/2;

        System.out.println("Cote : " + cote);

        int somme = values[0] +   values[1] +  values[2];

        g.fillOval(cote,0, cote,cote);
        if(somme > 0) {

            arcAngle = (int)(360 * ((float)values[0] /somme));
            g.setColor(new Color(255,0,0));
            g.fillArc(marge,0, cote,cote, startAngle, arcAngle);

            startAngle += arcAngle;

            arcAngle = (int)(360 * ((float)values[1] /somme));
            g.setColor(new Color(0,255,0));
            g.fillArc(marge,0, cote,cote, startAngle, arcAngle);

            startAngle += arcAngle;

            arcAngle = (int)(360 * ((float)values[2] /somme));
            g.setColor(new Color(0,0,255));
            g.fillArc(marge,0, cote,cote, startAngle, arcAngle);
        }
    }

    public void update(Triplet.FieldName fieldName, Integer newValue) {
        switch(fieldName){
            case A:
                values[0] = newValue;
                break;
            case B:
                values[1] = newValue;
                break;
            case C:
                values[2] = newValue;
                break;
        }

        Component c = this;
        while(c.getParent() != null) c = c.getParent();

        c.repaint();
    }
}
