package biduledres;

import java.awt.*;
import javax.swing.*;
import java.util.*;

public class RenduPolyedre extends JComponent {
    PolyedreSpatial p;
    public RenduPolyedre(PolyedreSpatial p) {
        this.p = p;
    }
    
    private int[] getXY(SommetSpatial s) {
        int x = (int) (s.getCoordonnee(0) * 200 + 200) ;
        int y = (int) (s.getCoordonnee(1) * 200 + 200) ;
        return new int[] {x, y};
    }
    
    private void drawArete(Graphics2D g, SommetSpatial s1, SommetSpatial s2) {
        int[] xy1 = getXY(s1);
        int[] xy2 = getXY(s2);

        double taille = 2 * (s1.getCoordonnee(2) + s2.getCoordonnee(2));
        g.setStroke(new BasicStroke((float) taille));
        
        g.drawLine(xy1[0], xy1[1], xy2[0], xy2[1]);
        drawSommet(g, s1);
        drawSommet(g, s2);
    }
    
    private void drawSommet(Graphics g, SommetSpatial s) {
        int[] xy = getXY(s);
        double taille = 10 * s.getCoordonnee(2);
        g.fillOval((int) Math.round(xy[0] - taille / 2), (int) Math.round(xy[1] - taille / 2), (int) Math.round(taille), (int) Math.round(taille));
    }
    
    
    public void drawAretes(Graphics2D g) {
        Iterator i = p.getListeAreteSpatiale().iterator();
        while(i.hasNext()) {
            AreteSpatiale areteSpatiale = (AreteSpatiale) i.next();
            SommetSpatial s1 = areteSpatiale.getSommetSpatial1();
            SommetSpatial s2 = areteSpatiale.getSommetSpatial2();
            drawArete(g, s1, s2);
        }
    }
    
    public void drawSommets(Graphics2D g) {
        Iterator i = p.getListeSommetSpatial().iterator();
        while(i.hasNext()) {
            SommetSpatial s = (SommetSpatial) i.next();
            drawSommet(g, s);
        }
    }
    
    
    public void paint(Graphics g) {
        Graphics2D g2D = (Graphics2D) g;
        g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2D.setBackground(Color.WHITE);
        g2D.clearRect(0, 0, getWidth(), getHeight());
        drawAretes(g2D);
    }
}