import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;

public class CatenaryView extends JPanel {

	private CatenaryModel catenary;
	public CatenaryView(CatenaryModel analysis) {
		setCatenaryModel(analysis);
	}
	
	public void setCatenaryModel(CatenaryModel analysis){
		catenary = analysis;
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;

		// Get outputs from Catenary object
		double xPole = catenary.getXPole();
		double yPole = catenary.getYPole();
		double w = catenary.getW();
		double yMin = catenary.getYMin();
		double T = catenary.getT();
		
		// Set font for text
		g2.setFont(new Font("Arial", 1, 11));

		// Draw the labels for the figure with the catenary.
		// The axes and poles will always be drawn at the same location
		// so the labels can be in fixed places
		g2.drawString("x   ", 40, 500);
		g2.drawString("y   ", 470, 100);
		g2.drawString("Pole at -" + xPole, 100, 500);
		g2.drawString("Pole at " + xPole, 780, 500);
		g2.drawString("Pole height " + yPole, 100, 100);
		g2.drawString("Pole height " + yPole, 780, 100);
		g2.drawString("(0,0)", 450, 500);
		g2.drawString("Tension = "+T, 400, 50);

		// Draw axes and poles
		g2.setStroke(new BasicStroke(1));
		g2.drawLine(40, 480, 860, 480); // x axis
		g2.drawLine(450, 480, 450, 80); // y axis
		g2.setPaint(Color.blue);
		Shape pole1 = new Rectangle2D.Double(100, 120, 20, 360);
		g2.draw(pole1);
		g2.fill(pole1);
		Shape pole2 = new Rectangle2D.Double(780, 120, 20, 360);
		g2.draw(pole2);
		g2.fill(pole2);
		g2.setPaint(Color.red);
		
		// Draw catenary as series of short line segments
		// Draw catenary from x= 0 to xPole
		final int NBR_POINTS= 100;
		int[] xDraw= new int[NBR_POINTS];
		int[] yDraw= new int[NBR_POINTS];
		for (int i= 0; i < NBR_POINTS; i++) {
			double x= xPole * i/ (NBR_POINTS-1);
			double y= catenary.getY(x);
			xDraw[i]= 450 + (int)(3.5*i);
			yDraw[i]= (int)(480 - 360*y/yPole);
		}
		g2.drawPolyline(xDraw, yDraw,NBR_POINTS);

		// Draw catenary from x= -xPole to 0
		int[] xDraw2= new int[NBR_POINTS];
		int[] yDraw2= new int[NBR_POINTS];
		for (int i= 0; i < NBR_POINTS; i++) {
			xDraw2[i]= 900 - xDraw[NBR_POINTS-1-i];
			yDraw2[i]= yDraw[NBR_POINTS-1-i];
		}
		g2.drawPolyline(xDraw2, yDraw2,NBR_POINTS);
	}
}