package DFT;
import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JPanel;
 
public class Graph extends JPanel{
	
	private static final long serialVersionUID = 1L;
	private float[] dft;
	
	public Graph(){
		dft = new float[0];
	}
	
	public void update(float[] dft){
		this.dft=dft;
		repaint();
	}
	
	public void paintComponent(Graphics g){
		/******* Background *******/
		g.setColor(new Color(250,250,250));
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
		
		
		/******* DFT Part *******/
		float space = (float)(this.getWidth()-30)/(float)dft.length;
    	float heightScale;
    	// Search max
    	float max=0,indexMax=0,tmp=0;
    	for (int i=0; i<dft.length; i++){
    		max=Math.max(max, dft[i]);
    		if (max!=tmp){
    			indexMax=i;
    			tmp=max;
    		}
    	}
    	heightScale = (float)(this.getHeight()-50)/max;
    	// Coordinates
    		g.setColor(Color.RED);
    		// Horizontal (f)
    		g.drawLine(18, this.getHeight()-20, this.getWidth()-10, this.getHeight()-20);
    		g.drawLine(this.getWidth()-10, this.getHeight()-20, this.getWidth()-15, this.getHeight()-15);
    		g.drawLine(this.getWidth()-10, this.getHeight()-20, this.getWidth()-15, this.getHeight()-25);
    		g.drawString("N",this.getWidth()-10,this.getHeight()-5);
    		// Vertical (mag)
    		g.drawLine(18, this.getHeight()-20, 18, 20);
    		g.drawLine(18, 20, 13, 20+5);
    		g.drawLine(18, 20, 23, 20+5);
    		g.drawString("mag",2,15);
    		g.drawString((int)max+"",(int)(indexMax*space)+25,this.getHeight()-(int)(max*heightScale)-20);
    		// Vert scale
    		
    	// DFT
    	g.translate(20, -20);
    	g.setColor(Color.BLACK);
        for (int i=0; i<dft.length; i++) g.fillRect((int)(i*space), this.getHeight()-(int)(dft[i]*heightScale), 1, (int)(dft[i]*heightScale));
        g.translate(-20, 20);
    }
}