package av2010;

import java.awt.Graphics;
import java.util.LinkedList;

import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * 
 * @author lombardy
 * Exercice 3
 */

public class Graphe extends JPanel{
	private static final long serialVersionUID = 1L;

	private final LinkedList<Fonction> fonctions = new LinkedList<Fonction>();
	private final double xmin;
	private final double xmax;
	private final double ymin;
	private final double ymax;

	public Graphe(double xmin, double xmax, double ymin, double ymax) {
		this.xmin = xmin;
		this.xmax = xmax;
		this.ymin = ymin;
		this.ymax = ymax;
		JFrame frame=new JFrame("Graphe");
		frame.setSize(200, 100);
		frame.setContentPane(this);
		frame.setResizable(true);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		frame.setVisible(true);
	}
	
	public void afficheFonction(Fonction f){
		fonctions.add(f);
		repaint();
	}
	
	@Override
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		int w=getWidth(), h=getHeight();
		double dx=w/(xmax-xmin);
		double dy=h/(ymax-ymin);
		for(Fonction f : fonctions){
			double ox=xmin, oy =f.evalue(ox);
			int oj=(int)((ymax-oy)*dy);
			for(int i=1; i<w; i++){
				double x=i/dx+xmin, y=f.evalue(x);
				int j=(int)((ymax-y)*dy);
				g.drawLine(i-1, oj, i, j);
				oj=j;
			}
		}
	}
}
